You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ApplicationContext fails to load in tests using Java-based config and WebMvcConfigurationSupport [SPR-9799] #14432
Test class extends AbstractTestNGSpringContextTests and uses @ContextConfiguration. It ran fine with XML-based configuration, but when I switched to Java-based configuration (with WebMvcConfigurationSupport) it fails.
Excerpt from the stacktrace
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
Analysis
There is no ServletContext when running tests. So the message makes sense, but I thought that using @EnableWebMvc or WebMvcConfigurationSupport was equivalent to using <mvc:annotation-driven />, but apparently it is not.
I could extend the WebMvcConfigurationSupport and override the defaultServletHandlerMapping() method as a work-around, but I don't think that should be necessary.
Further References
See reference URL for another person having the same problem.
This should work once proper support for WebApplicationContext's is available (SPR-9489). I am not sure how your XML configuration worked since there is no support loading a WebApplicationContext yet.
I think this bug is not "minor". You can not develop any integration test with Spring 3.1 + annotations. That is, if you are not use the old-fashioned spring xml configuration files.
I have tried 3.2.RC1 that includes the fixing of #9917 and, if I am not wrong, the bug still exists.
I have tried 3.2.RC1 that includes the fixing of #9917 and, if I am not wrong, the bug still exists.
When running your test against the Spring 3.2 RC1 snapshot, did you annotate your test class with the new @WebAppConfiguration annotation?
You'll need that to make the test work, since the presence of @WebAppConfiguration is what ensures that a WebApplicationContext will be loaded for your tests.
As pointed out in the Analysis section of this issue, the use of @EnableWebMvc is not equivalent to the use of <mvc:annotation-driven />. Specifically, @EnableWebMvc imports DelegatingWebMvcConfiguration which extends WebMvcConfigurationSupport, and that in turn requires that the ServletContext be available. In other words, when using @EnableWebMvc the application context must be a WebApplicationContext (WAC).
Although the use of <mvc:annotation-driven /> naturally implies that the application context must be a WAC, it does not necessarily require that the application context be a WAC for certain testing scenarios that avoid accessing web resources. That's why your tests passed when using XML but not when using configuration classes and @EnableWebMvc.
In summary, for proper integration testing of Spring Web MVC configuration using either <mvc:annotation-driven /> or @EnableWebMvc, you must ensure that the application context loaded for your tests is in fact a WebApplicationContext. This is made possible in Spring 3.2 via the new @WebAppConfiguration annotation. See #9917 for details.
Activity
spring-projects-issues commentedon Sep 14, 2012
Rossen Stoyanchev commented
This should work once proper support for WebApplicationContext's is available (SPR-9489). I am not sure how your XML configuration worked since there is no support loading a WebApplicationContext yet.
spring-projects-issues commentedon Sep 17, 2012
Sam Brannen commented
FYI: this issue depends on #9917.
Once #9917 is resolved, the problem described in this issue will likely no longer apply; however, we are keeping this issue open for the time being.
spring-projects-issues commentedon Oct 21, 2012
Guido García commented
I think this bug is not "minor". You can not develop any integration test with Spring 3.1 + annotations. That is, if you are not use the old-fashioned spring xml configuration files.
I have tried 3.2.RC1 that includes the fixing of #9917 and, if I am not wrong, the bug still exists.
spring-projects-issues commentedon Oct 31, 2012
Sam Brannen commented
Hi Guido,
When running your test against the Spring 3.2 RC1 snapshot, did you annotate your test class with the new
@WebAppConfigurationannotation?You'll need that to make the test work, since the presence of
@WebAppConfigurationis what ensures that aWebApplicationContextwill be loaded for your tests.Regards,
Sam
spring-projects-issues commentedon Oct 31, 2012
Sam Brannen commented
Hi guys,
As pointed out in the Analysis section of this issue, the use of
@EnableWebMvcis not equivalent to the use of<mvc:annotation-driven />. Specifically,@EnableWebMvcimportsDelegatingWebMvcConfigurationwhich extendsWebMvcConfigurationSupport, and that in turn requires that theServletContextbe available. In other words, when using@EnableWebMvcthe application context must be aWebApplicationContext(WAC).Although the use of
<mvc:annotation-driven />naturally implies that the application context must be a WAC, it does not necessarily require that the application context be a WAC for certain testing scenarios that avoid accessing web resources. That's why your tests passed when using XML but not when using configuration classes and@EnableWebMvc.In summary, for proper integration testing of Spring Web MVC configuration using either
<mvc:annotation-driven />or@EnableWebMvc, you must ensure that the application context loaded for your tests is in fact aWebApplicationContext. This is made possible in Spring 3.2 via the new@WebAppConfigurationannotation. See #9917 for details.Regards,
Sam
spring-projects-issues commentedon Oct 31, 2012
Sam Brannen commented
Resolving this issue as Works as Designed.
See previous comments and #9917 for further details.
spring-projects-issues commentedon Nov 1, 2012
Guido García commented
@SamBrannen it worked with the@WebAppConfigurationannotation. Thank you.spring-projects-issues commentedon Nov 2, 2012
Sam Brannen commented
Guido García,
That's good news, and thanks for providing feedback!