Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent logging noise during startup in @SpringBootTest?

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

As a result of the simple test above, I'm getting much startup noise logged. This was not the case before upgrading to spring-boot-2.x. How can I prevent this noise?

Especially, my Intellij IDE logs those statements in red, which is even more confusing as the test itself passes...

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]

Maybe it has to do using 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>
like image 987
membersound Avatar asked Jul 31 '18 12:07

membersound


3 Answers

Edited

Create logback-test.xml in your test resource (\test\resources), Then put bellow snippet into it

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

And dependency of test can be as follows

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
like image 107
Mehrdad HosseinNejad Yami Avatar answered Nov 19 '22 04:11

Mehrdad HosseinNejad Yami


You will need to create one logback-test.xml in your \test\resources directory, contents of this file can be following

<?xml version="1.0" encoding="UTF-8"?>
<configuration />

Why empty ?

Because you'd not want anything to be logged at this point in time, hence - empty configuration.

If you wish to find out more specific you can see some examples here

like image 32
Milan Desai Avatar answered Nov 19 '22 02:11

Milan Desai


First of all: I know, this is not a real answer, but way too long for a comment.

Using the log4j dependency didn't changed much in my output.

My POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

My application.properties is empty, so no log config there.

My test

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "logging.level.root=")
public class DemoApplicationTests
{

    @Test
    public void test()
    {
        System.out.println("Katzenbilder sind doof");
    }

}

Running the tests from within IntelliJ with logging.level.root=OFF leads to this output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

##### Optional[Hallo Welt]
##### Hallo Welt
Katzenbilder sind doof

Process finished with exit code 0

The two lines with #####are System.out.println() from a test bean.

Running the test with logging.level.root=INFO I get the expected clutter of Spring log messages.

I also, just for the sake of validation, put logging.level.root=INFO in my application.properties and had it to OFF in the test. No clutter, just the System.out.println() messages.

like image 1
Korashen Avatar answered Nov 19 '22 03:11

Korashen