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
Please sign in to leave a comment.
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?
Hello Yann,
target folder contains servlet-context.xml
Could you share your project? Or paste the test class' configuration for Spring?
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:
servlet-context.xml file is placed in: webapp_mvn_module/src/main/webapp/WEB-INF/ folder.
This time IDEA detects servlet-context.xml file but when I run DashboardControllerTest class from IDEA I get:
/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
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).
Yann, I just have felt that you will help me!
I have changed my abstract class to:
and test works properly in IDEA but not from maven command: mvn clean install
So I left right this:
and I edited test configuration, changing 'Working directory'
and finally tests can be run in both cases.
Yann, thanks!
greetings
Sławek
Yes, your solution makes sense. Glad I could be of help ;-)
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.