spring-boot - Spring Boot 单元测试忽略logging.level

标签 spring-boot logging logback

运行测试时,我的一个 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

我的应用程序记录很多,尤其是在加载上下文时。我尝试了 logback.xmllogback-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>

一个简单的测试类:

@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,但在另一个模块中它会忽略......但现在它对我来说是这样。

但也许一些背景知识的提示仍然受欢迎。

我没有将我的答案标记为解决方案,因为它仍然感觉像是一种解决方法。

关于spring-boot - Spring Boot 单元测试忽略logging.level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232827/

相关文章:

java - 如何为Azure托管服务指定elasticsearch spring boot配置(无端口)

c# - 自动记录和统计(C#应用程序)

logging - 如何更改 F# Saturn Framework 中的日志级别?

java - 如何扩展Springboot logback配置? (default.xml 显然不可用)

java - 从 Logback 中的 Appender 获取当前布局?

angular - 如何在 Spring Boot Jar 中集成 Angular 7 应用程序?

java - Controller 的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他 Controller

java - Spring Boot 记录到文件不起作用

scala - 如何在 sbt 测试中覆盖 application.conf 中的日志级别?

logback - 如何为Mybatis配置登录以打印我的SQL