Skip to content

ApplicationContext fails to load in tests using Java-based config and WebMvcConfigurationSupport [SPR-9799] #14432

Closed

Description

@spring-projects-issues

Per Holmberg opened SPR-9799 and commented

Overview

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.

Affects: 3.1.2

Reference URL: http://forum.springsource.org/showthread.php?123151-Missing-servlet-context-in-spring-tests

Issue Links:

1 votes, 4 watchers

Activity

  1. spring-projects-issues commented on Sep 14, 2012

    @spring-projects-issues
    CollaboratorAuthor

    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.

  2. spring-projects-issues commented on Sep 17, 2012

    @spring-projects-issues
    CollaboratorAuthor

    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.

  3. spring-projects-issues commented on Oct 21, 2012

    @spring-projects-issues
    CollaboratorAuthor

    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.

  4. spring-projects-issues commented on Oct 31, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Hi Guido,

    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.

    Regards,

    Sam

  5. spring-projects-issues commented on Oct 31, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Hi guys,

    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.

    Regards,

    Sam

  6. spring-projects-issues commented on Oct 31, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Resolving this issue as Works as Designed.

    See previous comments and #9917 for further details.

  7. spring-projects-issues commented on Nov 1, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Guido García commented

    @Sam Brannen it worked with the @WebAppConfiguration annotation. Thank you.

  8. spring-projects-issues commented on Nov 2, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Guido García,

    That's good news, and thanks for providing feedback!

  9. added
    type: bug
    status: declined
    in: test
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: teststatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions