programing

스프링 부트 유닛 테스트에서는 logging.level이 무시됩니다.

lastmemo 2023. 2. 20. 23:53
반응형

스프링 부트 유닛 테스트에서는 logging.level이 무시됩니다.

maven 모듈 중 하나가 테스트 실행 시 로깅레벨을 무시합니다.

src/test/resources있습니다application.properties:

app.name=bbsng-import-backend
app.description=Import Backend Module for Application
spring.profiles.active=test

# LOGGING
logging.level.root=error
logging.level.org.springframework.core =fatal
logging.level.org.springframework.beans=fatal
logging.level.org.springframework.context=fatal
logging.level.org.springframework.transaction=error
logging.level.org.springframework.test=error
logging.level.org.springframework.web=error
logging.level.org.hibernate=ERROR

나도 노력했어application-test.properties.

특히 컨텍스트를 로드할 때 My Application 로그가 많이 기록됩니다.나는 노력했다.logback.xml,logback-test.xml그리고.logback-spring.xml아무 도움도 안 돼요

내 폼:

<parent>
    <groupId>at.company.bbsng</groupId>
    <artifactId>bbsng-import</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>

<properties>
    <start-class>at.company.bbsng.dataimport.ApplicationImportBackend</start-class>
</properties>


<dependencies>

    <!-- APPLICATION ... -->
    <dependency>
        <groupId>at.company.bbsng</groupId>
        <artifactId>bbsng-app-domain</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SPRING ... -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</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-data-jpa</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- JAVAX ... -->
       ...

    <!-- COMMONS ... -->
       ...

    <!-- LOMBOK ... -->
       ...

    <!-- DB -->
       ...

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${org.springframework.boot-version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

간단한 테스트 클래스 1개:

@ContextConfiguration(classes = { ApplicationImportBackend.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {

    @Autowired
    private JobLauncher jobLauncher;

    @Test
    public void testSimpleProperties() throws Exception {
        assertNotNull(jobLauncher);
    }

}

응용 프로그램 로그가 DEBUG 모드입니다.

그리고 네, 그리고application.properties로딩됩니다.이미 잘못된 설정으로 어플리케이션을 중단하려고 했습니다.

힌트 주셔서 감사합니다.

다음으로 설정한 모든 모듈에서 수행한 작업은 다음과 같습니다.

src/main/main:
에서 로깅 설정을 사용합니다.application.properies맘에 들다logging.level.*질문에 기재된 바와 같이

src/test/module:
사용하고 있다logback-test.xml예를 들어 다음과 같습니다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="*.myapp" level="error" />
    <logger name="org.springframework.core " level="error" />
    <logger name="org.springframework.beans" level="error" />
    <logger name="org.springframework.context" level="error" />
    <logger name="org.springframework.transaction" level="error" />
    <logger name="org.springframework.web" level="error" />
    <logger name="org.springframework.test" level="error" />
    <logger name="org.hibernate" level="error" />
</configuration>

하지만 몇 개의 모듈에서는 application.properties를 사용할 수 있지만 다른 모듈에서는 무시되는 이유를 이해할 수 없습니다.하지만 지금으로서는 그게 나한테는 효과가 있어.

그러나 배경지식이 있는 힌트는 아직 별로 환영받지 못할지도 모른다.

아직 회피책으로 느껴지기 때문에 해결책으로 표시하지 않습니다.

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

빠른 해결책으로, 저는logback.xml상기의 내용을 파일에 기입하다src/test/resources그리고 그것은 동작한다.

유효하게 하려면application.properties주석을 추가할 필요가 있다@SpringBootTest자세한 내용은 여기를 참조하십시오.

또한 이에 대한 솔루션을 찾고 있으며, 동시에 다음과 같은 솔루션을 사용하고 있습니다.

이것은 최고는 아니지만 효과가 있다

@BeforeClass
public static void setErrorLogging() {
   LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
}

LoggingSystem: 로깅 시스템에 대한 공통 추상화.

->

get: 사용 중인 로깅 시스템을 감지하여 반환합니다.로그백 및 Java 로깅 지원

setLogLevel: 지정된 로거의 로깅레벨을 설정합니다.

다른 모든 테스트 클래스에 대해 로그 수준을 다시 변경하십시오.

도움이 되길 바래, 행운아

테스트에 주석이 붙어 있는 경우@DataJpaTest다음을 사용하여 휴지 상태 SQL 로그오프 스위치를 끌 수 있습니다.

@DataJpaTest(showSql=false)
public class MyTest {
  ..
}

이것을 시험해 보세요.

@ContextConfiguration(classes = ApplicationImportBackend.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {
    //...
}

언급URL : https://stackoverflow.com/questions/35232827/spring-boot-unit-test-ignores-logging-level

반응형