Spring Framework
  1. Spring Framework
  2. SPR-9799

ApplicationContext fails to load in tests using Java-based config and WebMvcConfigurationSupport

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Works as Designed
    • Affects Version/s: 3.1.2
    • Fix Version/s: None
    • Component/s: Test
    • Labels:
      None

      Description

      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.

        Issue Links

          Activity

          Hide
          Rossen Stoyanchev added a comment -

          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.

          Show
          Rossen Stoyanchev added a comment - 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.
          Hide
          Sam Brannen added a comment -

          FYI: this issue depends on SPR-5243.

          Once SPR-5243 is resolved, the problem described in this issue will likely no longer apply; however, we are keeping this issue open for the time being.

          Show
          Sam Brannen added a comment - FYI: this issue depends on SPR-5243 . Once SPR-5243 is resolved, the problem described in this issue will likely no longer apply; however, we are keeping this issue open for the time being.
          Hide
          Guido García added a comment -

          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 SPR-5243 and, if I am not wrong, the bug still exists.

          Show
          Guido García added a comment - 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 SPR-5243 and, if I am not wrong, the bug still exists.
          Hide
          Sam Brannen added a comment -

          Hi Guido,

          I have tried 3.2.RC1 that includes the fixing of SPR-5243 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

          Show
          Sam Brannen added a comment - Hi Guido, I have tried 3.2.RC1 that includes the fixing of SPR-5243 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
          Hide
          Sam Brannen added a comment -

          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 SPR-5243 for details.

          Regards,

          Sam

          Show
          Sam Brannen added a comment - 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 SPR-5243 for details. Regards, Sam
          Hide
          Sam Brannen added a comment -

          Resolving this issue as Works as Designed.

          See previous comments and SPR-5243 for further details.

          Show
          Sam Brannen added a comment - Resolving this issue as Works as Designed . See previous comments and SPR-5243 for further details.
          Hide
          Guido García added a comment -

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

          Show
          Guido García added a comment - @Sam Brannen it worked with the @WebAppConfiguration annotation. Thank you.
          Hide
          Sam Brannen added a comment - - edited

          Guido García,

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

          Show
          Sam Brannen added a comment - - edited Guido García , That's good news, and thanks for providing feedback!

            People

            • Assignee:
              Sam Brannen
              Reporter:
              Per Holmberg
              Last updater:
              Sam Brannen
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 24 weeks ago