Skip to main content
 首页 » 编程设计

java之如何在@SpringBootTest 启动期间防止记录噪音

2023年09月11日25softidea

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 
                    properties = "logging.level.root=OFF") 
public class MyTest { 
         @Test 
         public void test() {} 
} 

作为上述简单测试的结果,我记录了很多启动噪音。在升级到 spring-boot-2.x 之前情况并非如此。 我怎样才能防止这种噪音?

特别是,我的 Intellij IDE 将这些语句记录为红色,这在测试本身通过时更加令人困惑...

Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration 
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [MyTest], using SpringBootContextLoader 
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations 
INFO: Could not detect default resource locations for test class [MyTest]: no resource found for suffixes {-context.xml, Context.groovy}. 
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses 
INFO: Could not detect default configuration classes for test class [MyTest]: MyTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 
Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getOrFindConfigurationClasses 
INFO: Found @SpringBootConfiguration MyApp for test class MyTest 
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getDefaultTestExecutionListenerClassNames 
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.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, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.security.test.context.support.ReactorContextTestExecutionListener] 
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners 
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1a4013, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1b6e1eff, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@306f16f3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@702b8b12, org.springframework.test.context.support.DirtiesContextTestExecutionListener@22e357dc, org.springframework.test.context.transaction.TransactionalTestExecutionListener@49912c99, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@10163d6, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@2dde1bff, org.springframework.security.test.context.support.ReactorContextTestExecutionListener@15bbf42f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@550ee7e5, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@5f9b2141, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@247d8ae, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@48974e45, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@6a84a97d] 

也许与使用 log4j2 有关?

 <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter</artifactId> 
        <exclusions> 
            <exclusion> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-starter-logging</artifactId> 
            </exclusion> 
        </exclusions> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-log4j2</artifactId> 
    </dependency> 

请您参考如下方法:

已编辑

在你的测试资源(\test\resources)中创建logback-test.xml, 然后将波纹管片段放入其中

<configuration> 
    <include resource="org/springframework/boot/logging/logback/base.xml" /> 
    <logger name="org.springframework" level="OFF"/> 
</configuration> 

It prints default system INFOs, not startup noise logs. (As you wanted)

测试的依赖可以如下

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <scope>test</scope> 
</dependency>