1

I'm trying to configure JUnit to work with Spring, but I cannot.

I create a test class this way:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/web-application-config.xml"})
public class MyControllerToTest {
        @Autowired
    private MyController ctr;

    @Test
    ....
}

In classpath, I inserted src/main/resources, where the web-application-config.xml is located. Here its content:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.infoone.mycontrollerpackage"/>

<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webflow-config.xml" />
<import resource="webmvc-config.xml" />
<import resource="data-access-config.xml" />
<import resource="repository-config.xml" />
<import resource="security-config.xml" />

When I run as JUnit the MyControllerToTest class, I get the following:

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6419fa] to prepare test instance [com.infoone.myapp.MyControllerToTest@10036f2]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowExecutor': Cannot resolve reference to bean 'flowRegistry' while setting bean property 'flowDefinitionLocator'; nested exception is         org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowRegistry': Invocation of init method failed; nested exception is java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.lang.IllegalStateException: An I/O Exception occurred resolving the flow location pattern '/**/*-flow.xml'
...
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/flows/] cannot be resolved to URL because it does not exist[/code]

Thank you!

0

2 Answers 2

3

The Problem is that the WEB-INF folder is not (so easy) accessible while testing (with maven), because it is in an other location while testing.

I mean:

  • when you run the tests, you run the compiled classes in folder: <project>/target/test-classes and the WEB-INF folder is located in <project>/target
  • when you run the application the web-inf located in <war>/WEB-INF while your classes resists in <war>/WEB-INF/classes

What I want to say, the relative path to the resource in WEB-INF folder is different between test and application.

Anyway there is a spring project called spring-test-mvc, maybe you can use it or use some ideas you find in there code.

2
  • Thank you, very helpful. Would you suggest to move flows out of WEB-INF? I could move them to src/main/resources, but, in that case, I'm not sure how to refer them from my web pages...
    – Manu
    Sep 26, 2012 at 14:52
  • 1
    @Manu: the content of src/main/resources is merged into the classpath root of your application while building the app.
    – Ralph
    Dec 12, 2012 at 7:56
0

You need to add file:src inside @ContextConfiguration and add @WebAppConfiguration annotation. So the the test class should look like this:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"file:src:/path.../web-application-config.xml"})
public class MyControllerToTest {
        @Autowired
    private MyController ctr;

    @Test
    ....
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.