Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9799

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

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: 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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sbrannen Sam Brannen
                Reporter:
                pehol 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:
                  5 years, 33 weeks, 6 days ago