我看到我的大学升级了Spring应用程序(1.5-> 2.1)并替换了
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
与
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
因为不推荐运行者软件包中的Mockito https://www.baeldung.com/mockito-deprecated-mockitojunitrunner。 使用最直接的方法,他应该按照文章中的说明切换软件包,但他将其更改为完全不同的运行程序,即SpringRunner。
如果您阅读以下答案 https://stackoverflow.com/a/49647467/978302
您会发现SpringRunner只是加载整个Spring上下文,在轻量级单元测试中不建议这样做。 无论如何,如果您使用SpringRunner做到这一点,请注意我需要一条规则来启用@Mock注释。
我很好奇,这种模仿注释仅适用于SpringRunner测试。
在日志中,我看到很多测试执行监听器都已加载。
2019-03-15T15:14:04.728Z INFO org.springframework.test.context.support.DefaultTestContextBootstrapper [main] () : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.xxx.order.platforms.tasks.ExportSellerTaskTest], using DelegatingSmartContextLoader
2019-03-15T15:14:04.735Z INFO org.springframework.test.context.support.AbstractContextLoader [main] () : Could not detect default resource locations for test class [com.xxx.order.platforms.tasks.ExportSellerTaskTest]: no resource found for suffixes {-context.xml}.
2019-03-15T15:14:04.737Z INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils [main] () : Could not detect default configuration classes for test class [com.xxx.order.platforms.tasks.ExportSellerTaskTest]: ExportSellerTaskTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2019-03-15T15:14:04.788Z INFO org.springframework.test.context.support.DefaultTestContextBootstrapper [main] () : 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.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
2019-03-15T15:14:04.802Z INFO org.springframework.test.context.support.DefaultTestContextBootstrapper [main] () : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@443118b0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@765d7657, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@74235045, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@618b19ad, org.springframework.test.context.support.DirtiesContextTestExecutionListener@2d3379b4, org.springframework.test.context.transaction.TransactionalTestExecutionListener@30c15d8b, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5e0e82ae, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@6771beb3, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@51399530, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@6b2ea799, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@411f53a0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@2b71e916]
其中之一也是 MockitoTestExecutionListener 。这就是为什么@Mock注释开箱即用的原因? 是否由于在项目中使用 spring-boot-starter-test 而加载了此侦听器? https://github.com/spring-projects/spring-boot/blob/d5a197fe66285a9e357125f14febbb3514b97d7c/spring-boot-project/spring-boot-test/src/main/resources/META-INF/spring.factories
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.1.1.RELEASE:test
[INFO] | +- org.mockito:mockito-core:jar:2.23.4:test
测试示例
@RunWith(SpringRunner.class)
public class SomeTest {
@Mock
SomeClass someClass;
@Test
public void smokeTest() {
}
}
问:如果已经依赖 spring-boot-starter-test ,则可以使用 SpringRunner 注释测试类,而无需手动进行注释接线,可以使用Mockito。 另外,如果不指定特定的Context,将只设置TestExecutionListeners,测试仍然是超轻量级的,对吧?