java – 为什么组件扫描不适用于Spring Boot单元测试?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 为什么组件扫描不适用于Spring Boot单元测试?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5267字,纯文字阅读大概需要8分钟。
内容图文
服务类FooServiceImpl使用@Service aka @Component进行注释,这使其有资格进行自动装配.为什么在单元测试期间没有拾取和自动装配此类?
@Service
public class FooServiceImpl implements FooService {
@Override
public String reverse(String bar) {
return new StringBuilder(bar).reverse().toString();
}
}
@RunWith(SpringRunner.class)
//@SpringBootTest
public class FooServiceTest {
@Autowired
private FooService fooService;
@Test
public void reverseStringShouldReverseAnyString() {
String reverse = fooService.reverse("hello");
assertThat(reverse).isEqualTo("olleh");
}
}
测试无法加载应用程序上下文,
2018-02-08T10:58:42,385 INFO Neither @ContextConfiguration nor @ContextHierarchy found for test class [io.github.thenilesh.service.impl.FooServiceTest], using DelegatingSmartContextLoader
2018-02-08T10:58:42,393 INFO Could not detect default resource locations for test class [io.github.thenilesh.service.impl.FooServiceTest]: no resource found for suffixes {-context.xml}.
2018-02-08T10:58:42,394 INFO Could not detect default configuration classes for test class [io.github.thenilesh.service.impl.FooServiceTest]: FooServiceTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2018-02-08T10:58:42,432 INFO Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, (...)org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
2018-02-08T10:58:42,448 INFO Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@f0ea28, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@16efaab,(...)org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@9604d9]
2018-02-08T10:58:42,521 INFO Refreshing org.springframework.context.support.GenericApplicationContext@173f9fc: startup date [Thu Feb 08 10:58:42 IST 2018]; root of context hierarchy
2018-02-08T10:58:42,606 INFO JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-02-08T10:58:42,666 ERROR Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@19aaa5] to prepare test instance [io.github.thenilesh.service.impl.FooServiceTest@57f43]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'io.github.thenilesh.service.impl.FooServiceTest': Unsatisfied dependency expressed through field 'fooService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.github.thenilesh.service.FooService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
. . .
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:?]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.github.thenilesh.service.FooService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 28 more
2018-02-08T10:58:42,698 INFO Closing org.springframework.context.support.GenericApplicationContext@173f9fc: startup date [Thu Feb 08 10:58:42 IST 2018]; root of context hierarchy
如果测试类用@SpringBootTest注释,那么它会创建整个应用程序上下文,包括数据库连接和许多不相关的bean,这显然不需要进行单元测试(那时它不会进行单元测试!).预期的是,除了使用@MockBean进行模拟之外,只应实例化FooService所依赖的bean.
解决方法:
你应该使用@SpringBootTest(classes = FooServiceImpl.class).
正如它在Annotation Type SpringBootTest提到的那样:
public abstract Class[] classes
The annotated classes to use for loading an ApplicationContext. Can
also be specified using @ContextConfiguration(classes=…). If no
explicit classes are defined the test will look for nested
@Configuration classes, before falling back to a
SpringBootConfiguration search.Returns:
the annotated classes used to load the application context See Also:
ContextConfiguration.classes()Default:
{}
这只会加载必要的类.如果不指定,它可能会加载数据库配置和其他会使您的测试更慢的东西.
另一方面,如果你真的想要单元测试,你可以在没有Spring的情况下测试这段代码 – 然后@RunWith(SpringRunner.class)和@SpringBootTest注释是没有必要的.您可以测试FooServiceImpl实例.如果您有自动装配/注入的属性或服务,则可以通过setter,构造函数或使用Mockito模拟它们.
内容总结
以上是互联网集市为您收集整理的java – 为什么组件扫描不适用于Spring Boot单元测试?全部内容,希望文章能够帮你解决java – 为什么组件扫描不适用于Spring Boot单元测试?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。