I noticed that with @EnableWebMvc
I'm getting the following error when running my tests: A ServletContext is required to configure default servlet handling
. This issue is temporarily resolved by commenting out @EnableWebMvc
then my tests all pass, however I want this in my web app.
I read in this post that I could put the @EnableWebMvc
in another config class that isn't included in the tests(?). So I've tried this:
AppConfig.java
@Configuration
@ComponentScan(basePackages = "biz.martyn.budget")
@PropertySource("classpath:prod.properties")
@EnableTransactionManagement
public class AppConfig {
@Autowired
private Environment env;
@Bean(name = "dataSource", destroyMethod = "shutdown")
@Profile("prod")
public DataSource dataSourceForProd() {...
WebMvcConfig.java
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {...
Then in my tests I'm attempting:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@Transactional
public class FundRepositoryTest {...
However, I'm still seeing the same error in my tests. I know it's the @EnableWebMvc
as they all pass when I remove this. Have I misunderstood something with how @ContextConfiguration annotation works? By the way, I'm using Spring version 4.2.2.RELEASE for all my spring-* dependencies if that helps.
Below is also the error I'm seeing in my test run:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)