Error creating bean with name defaultServletHandlerMapping Error creating bean with name defaultServletHandlerMapping spring spring

Error creating bean with name defaultServletHandlerMapping


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

e.g.

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


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) })


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>