使用SpringRunner.class

时间:2018-03-16 08:25:16

标签: java spring junit junit4 maven-surefire-plugin

我跟着this blog post使用@Category注释设置了包含单独的单元和集成测试的Maven构建。

在大多数情况下,这是有效的:标记为@Category(IntegrationTest.class)的测试在integration-test阶段执行,所有未标记的测试都在test阶段执行。

但是,在运行单元测试时,看起来集成测试的上下文仍在(部分?)创建,即使测试本身没有运行:

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ bamboo ---
[INFO] Surefire report directory: [removed]
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=0, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
09:56:05.458 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.MyIntegrationTest]
09:56:05.463 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
09:56:05.468 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
09:56:05.481 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.MyIntegrationTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
09:56:05.494 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.MyIntegrationTest], using SpringBootContextLoader
09:56:05.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.MyIntegrationTest]: class path resource [com/example/MyIntegrationTest-context.xml] does not exist
09:56:05.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.MyIntegrationTest]: class path resource [com/example/MyIntegrationTestContext.groovy] does not exist
09:56:05.497 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.MyIntegrationTest]: no resource found for suffixes {-context.xml, Context.groovy}.
09:56:05.530 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.MyIntegrationTest]
09:56:05.563 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.MyIntegrationTest]: using defaults.
09:56:05.567 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 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]
09:56:05.580 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
09:56:05.586 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@62fdb4a6, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@11e21d0e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1dd02175, org.springframework.test.context.transaction.TransactionalTestExecutionListener@31206beb, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@3e77a1ed, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@3ffcd140, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@23bb8443, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1176dcec, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@120d6fe6, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@4ba2ca36, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@3444d69d]
09:56:05.591 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.MyIntegrationTest]
09:56:05.591 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.MyIntegrationTest]
Running com.example.MyUnitTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.323 sec - in com.example.MyUnitTest

这是MyIntegrationTest注释的方式:

@Category(IntegrationTest.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApp.class)
public class MyIntegrationTest {
    @Autowired
    private ActualObject actualObject;
}

如何防止此行为?

1 个答案:

答案 0 :(得分:3)

这会创建一个TestContext,但不会对其进行初始化,因此它应该很快。

对于每个测试类,都会创建一个Spring TestContextManager,如SpringJUnit4ClassRunner.java的构造函数中所定义(与SpringRunner.java相同)。

除了实际排除测试文件本身之外,我认为你不能避免创建TestRunner。

您可以通过在test / resources文件夹中添加logback-test.xml来最好地抑制这些日志。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
</configuration>