"Spring Boot Unit Test忽略了logging.level"是说在Spring Boot的单元测试中,日志记录级别将被忽略。

10 浏览
0 Comments

"Spring Boot Unit Test忽略了logging.level"是说在Spring Boot的单元测试中,日志记录级别将被忽略。

我的某个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文件:

    at.company.bbsng
    bbsng-import
    0.1.0-SNAPSHOT

bbsng-import-backend
bbsng-import-backend

    at.company.bbsng.dataimport.ApplicationImportBackend


    
    
        at.company.bbsng
        bbsng-app-domain
        test
    
    
    
        org.springframework.boot
        spring-boot-starter-batch
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
        test
    
    
       ...
    
       ...
    
       ...
    
       ...


    
        
            org.springframework.boot
            spring-boot-maven-plugin
            ${org.springframework.boot-version}
            
                
                    
                        repackage
                    
                
            
        
    

一个简单的测试类:

@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将被加载。我已经尝试通过错误配置来破坏应用程序。

感谢任何提示。

admin 更改状态以发布 2023年5月23日
0
0 Comments



    
    

作为一个快速修复,我把具有上述内容的logback.xml文件放在src/test/resources中,它有效。

0
0 Comments

好的,我现在在所有模块中按以下方式配置:

src/main/resources:
我在application.properties中使用日志配置,如问题所述的logging.level.*

src/test/resources:
我使用logback-test.xml,如下:



    
    
    
    
    
    
    
    
    

但我仍然不明白为什么在一些模块中我可以使用application.properties,但在另一个模块中它会被忽略……但目前它对我来说可以工作。

但也许还是欢迎一些有背景知识的提示。

我不标记我的回答为解决方案,因为它仍然感觉像一个解决方法。

0