Error creating bean with name defaultServletHandlerMapping

For me after adding @WebAppConfiguration in the Test Class it worked.


Class AccountServiceTest must be injecting a bean with SpingMVC or declared in a config that has SpringMVC enabled. In you project pom.xml, add the javax.servlet-api dependency to the test scope.

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <scope>test</scope>
</dependency>

Your configuration is fine, except for one place

@ComponentScan(basePackages = { "org.example.springproject" })

I would assume that you have other @Configuration in your package, that's picked up by your @ComponentScan (DelegatingWebMvcConfiguration that appears in your exception is, most likely, imported by @EnableWebMvc somewhere in external @Configuration).

Possible solution is to use a filter in your component scan.

@ComponentScan(basePackages = { "org.example.springproject" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })


Add the @WebAppConfiguration and ALso, replace the ApplicationContextTest.class with AnnotationConfigWebContextLoader.class

e.g.

    @WebAppConfiguration
    @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, 
classes = { ApplicationContextTest.class})