1. ホーム
  2. spring-boot

[解決済み] Spring Bootのユニットテストはlogging.levelを無視する

2022-10-30 15:47:57

質問

私のMavenモジュールの1つは、テスト実行時に私のロギングレベルを無視します。

src/test/resourcesapplication.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 .

私のアプリケーションは、特にコンテキストをロードするときに、多くのログを記録します。試しに logback.xml , logback-test.xmllogback-spring.xml を追加しましたが、何の役にも立ちません。

私のpomです。

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

一つの簡単なTestクラス。

@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/resources。

私はロギング設定を application.properies のように logging.level.* のように、質問で説明されているように

src/test/resources:

私は 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を使用することができ、別のモジュールでは無視される.... しかし、今のところ、それはそのままの状態で私のために動作します。

しかし、多分、背景知識を持ついくつかのヒントはまだ歓迎されています。

私の回答は、解決策としてはマークしていません。