spring mvc tests do not run from IDEA

Hello,

recently I have just checked new spring module called: spring-test (from Spring 3.2 - yes I know IDEA 12 has support for 3.1).
I created simple tests but I could not run theme from IDEA (ctrl + shift + F10).

The same tests I can run from:

  • Eclipse STS and
  • Netbeans 7.3,
  • via mvn clean install or
  • mvn test -Dtest=ControllerNameTest from terminal (cmd)


but when I run them from IDEA I get:


java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.io.FileNotFoundException: src\main\webapp\WEB-INF\servlet-context.xml


I set context xml file path to:

@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-context.xml")


Issue occures on:
- Windows 7
- IDEA 12
- Spring 3.2

Greetings
Sławek

8 comments

How did you load/import your Maven project in IDEA? Could you verify /WEB-INF/ is treated correctly (marked as source??), so that /target/ contains necessary spring.xml?

0

Hello Yann,
target folder contains servlet-context.xml

0

Could you share your project? Or paste the test class' configuration for Spring?


0

Yann, thank you for tracking my issue.

I can not share project but I will describe my project structure.
By the way - I imported spring-mvc-showcase tests (https://github.com/SpringSource/spring-mvc-showcase) and tests included in this simple maven module works properly.

I have project with few modules. Structure looks like this:

project_mvn_module (pom.xml)
     |-module_folder
          |-server_folder
               |-domain_mvn_module (pom.xml)
               |-service_mvn_module (pom.xml)
               |-service_impl_maven_module (pom.xml)
               |-...
                             
          |-client_folder
               |-controller_mvn_module (pom.xml)
               |-webapp_mvn_module (pom.xml)
                    |-main, resources, webapp folder, WEB-INF
                    |-test, java folder
                         |-AbstractControllerTest.java
                         |-DashboardControllerTest.java



servlet-context.xml file is placed in: webapp_mvn_module/src/main/webapp/WEB-INF/ folder.   

AbstractControllerTest.java
---
@WebAppConfiguration
@ContextConfiguration("file:**/servlet-context.xml")
public class AbstractControllerTest {

    @Autowired
    protected WebApplicationContext wac;
}


DashboardControllerTest.java

---
@RunWith(SpringJUnit4ClassRunner.class)
public class DashboardControllerTest extends AbstractControllerTest {

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        mockMvc = webAppContextSetup(wac).alwaysDo(print()).alwaysExpect(status().isOk()).build();
    }

    @Test
    public void getDashboard() throws Exception {
        mockMvc.perform(get("/dashboard/"))
                .andExpect(forwardedUrl("/WEB-INF/view/layout/dashboard.jsp"))
                .andExpect(view().name("dashboard.page"))
                .andExpect(model().attributeExists("projects", "clients"));
    }
}



This time IDEA detects servlet-context.xml file but when I run DashboardControllerTest class from IDEA I get:

22:08:01.130 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7544ae20] to prepare test instance [com.saas.project_name.client.webapp.DashboardControllerTest@7003f305]
java.lang.IllegalStateException: Failed to load ApplicationContext
     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) [junit-4.8.2.jar:na]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) [junit-4.8.2.jar:na]
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) [junit-4.8.2.jar:na]
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) [junit-4.8.2.jar:na]
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) [junit-4.8.2.jar:na]
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) [junit-4.8.2.jar:na]
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.junit.runners.ParentRunner.run(ParentRunner.java:236) [junit-4.8.2.jar:na]
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) [spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.junit.runner.JUnitCore.run(JUnitCore.java:157) [junit-4.8.2.jar:na]
     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76) [junit-rt.jar:na]
     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) [junit-rt.jar:na]
     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) [junit-rt.jar:na]
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_09]
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_09]
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_09]
     at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_09]
     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in file [C:\Users\Slawek\Workspace\Saas\Project\module\client\webapp\target\webapp\WEB-INF\servlet-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: no URL for ServletContext resource [/WEB-INF/view/tile-defs.xml]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:116) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:59) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) ~[spring-test-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     ... 27 common frames omitted
Caused by: java.lang.IllegalArgumentException: no URL for ServletContext resource [/WEB-INF/view/tile-defs.xml]
     at org.springframework.web.servlet.view.tiles3.SpringWildcardServletTilesApplicationContext.getResources(SpringWildcardServletTilesApplicationContext.java:94) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.getSources(TilesConfigurer.java:302) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.apache.tiles.factory.BasicTilesContainerFactory.createLocaleDefinitionDao(BasicTilesContainerFactory.java:182) ~[tiles-core-3.0.1.jar:3.0.1]
     at org.apache.tiles.factory.BasicTilesContainerFactory.createDefinitionsFactory(BasicTilesContainerFactory.java:137) ~[tiles-core-3.0.1.jar:3.0.1]
     at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createDefinitionsFactory(TilesConfigurer.java:348) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.apache.tiles.factory.BasicTilesContainerFactory.createContainer(BasicTilesContainerFactory.java:86) ~[tiles-core-3.0.1.jar:3.0.1]
     at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createContainer(TilesConfigurer.java:293) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.apache.tiles.startup.AbstractTilesInitializer.createContainer(AbstractTilesInitializer.java:114) ~[tiles-core-3.0.1.jar:3.0.1]
     at org.apache.tiles.startup.AbstractTilesInitializer.initialize(AbstractTilesInitializer.java:64) ~[tiles-core-3.0.1.jar:3.0.1]
     at org.springframework.web.servlet.view.tiles3.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:256) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     ... 42 common frames omitted
Caused by: java.io.FileNotFoundException: ServletContext resource [/WEB-INF/view/tile-defs.xml] cannot be resolved to URL because it does not exist
     at org.springframework.web.context.support.ServletContextResource.getURL(ServletContextResource.java:154) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     at org.springframework.web.servlet.view.tiles3.SpringWildcardServletTilesApplicationContext.getResources(SpringWildcardServletTilesApplicationContext.java:90) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
     ... 53 common frames omitted


/WEB-INF/view/tile-defs.xml is entry in servlet-context.xml file and points to apache tiles definitions.

Once again, running tests of this project structure works properly under Eclipse STS (latest) and Netbeans 7.3.
Success is also when I run
- mvn clean install in: - project_mvn_module or in webapp_mvn_module
- mvn test -Dtest=DashboardControllerTest in webapp_mvn_module

Greetings
Sławek

0

It's a bit hard to tell what goes wrong without having the full project and all corresponding configurations.

Two guesses:
- try fixing/changing run configuration within IntelliJ IDEA of your test (see below)
- if that doesn't help, try applying @WebAppConfiguration to see if that fixes web root problem

I assume this is a "current directory" vs. "classpath" vs. "web root" problem caused by subtle differences in the way you start your test (since it does work from commandline the misconfiguration must be in IntelliJ IDEA).

0

Yann, I just have felt that you will help me!

I have changed my abstract class to:

@WebAppConfiguration("module/client/webapp/src/main/webapp")
@ContextConfiguration("file:**/servlet-context.xml")
public class AbstractControllerTest {

    @Autowired
    protected WebApplicationContext wac;
}


and test works properly in IDEA but not from maven command: mvn clean install

So I left right this:

@WebAppConfiguration
@ContextConfiguration("file:**/servlet-context.xml")
public class AbstractControllerTest {

    @Autowired
    protected WebApplicationContext wac;
}


and I edited test configuration, changing 'Working directory'

from: C:\Users\Slawek\Workspace\Saas\Project
  to: C:\Users\Slawek\Workspace\Saas\Project\module\client\webapp


and finally tests can be run in both cases.

Yann, thanks!

greetings
Sławek

0

Yes, your solution makes sense. Glad I could be of help ;-)


0

I can affirm, the solution works somehow when you give working directory as an absolute path to the module rather than selecting $ MODULE_WORKING_DIR$. This is true with Maven Project, src/main     src/test     configuration and I affirm with IntelliJ 2018.3 Ultimate Edition. I was getting FileNotFoundException when running Integration Tests  .\src\test\resources\conf\application.properties whereas that file is there.

Change the test run configuration: Working Directory from  $ MODULE_WORKING_DIR$ to C:\specific\path\to\your\module and it worked.

0

Please sign in to leave a comment.