Skip to content

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

Closed
@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions