Error creating bean with name defaultServletHandlerMapping

故事扮演 提交于 2019-11-27 22:52:28

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

user3798167

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

e.g.

    @WebAppConfiguration
    @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, 
classes = { ApplicationContextTest.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>

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

I recall from a recent project where we could not use the Spring jUnit Runner for a web application. We had to remove Sprint jUnit runner. We then supplemented it with loading spring ourselves in a TestCase derived base class.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!