Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Unit Test ignores logging.level

One of my maven module ignores my logging levels when running tests.

In src/test/resources I have 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 

I also tried application-test.properties.

My Application logs a lot, especially when loading context. I tried logback.xml, logback-test.xml and logback-spring.xml but nothing helps.

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

One simple Test class:

@ContextConfiguration(classes = { ApplicationImportBackend.class }) @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles({ "test" }) public class BatchJobConfigurationTests {      @Autowired     private JobLauncher jobLauncher;      @Test     public void testSimpleProperties() throws Exception {         assertNotNull(jobLauncher);     }  } 

Application logs is in DEBUG Mode.

And yes, the application.properties will be loaded. I already tried to break the application by wrong config.

Thank you for any hints.

like image 203
Michael Hegner Avatar asked Feb 05 '16 20:02

Michael Hegner


People also ask

How do I change the log level in Junit?

how to change the log level for certain junit test methods. The way to go is to use a custom junit MethodRule that accesses the loggers and re-configures the log level per package. With below classes, you can achieve this.

How do I change spring boot logging level?

In Settings -> Config Vars set logging. level.com. yourpackage to the desired level (INFO, ERROR, DEBUG).

How does spring Boot define logging level?

properties file, you can define log levels of Spring Boot loggers, application loggers, Hibernate loggers, Thymeleaf loggers, and more. To set the logging level for any logger, add properties starting with logging. level . Logging level can be one of one of TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF .

What is default logging level in spring boot?

INFO - INFO is the default logging level that is set by Spring Boot.


1 Answers

Okay what I did now, in all modules I configured as follows:

src/main/resources:
I use logging configuration in application.properies like logging.level.* as described in the question.

src/test/resources:
I use logback-test.xml like:

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

But I still don't understand, why in few modules I could use application.properties, but in another module it ignores ... But for now it works for me as it is.

But maybe few hints with background knowledge are still welcome.

I dont mark my answer as solution, cos it still feels like a workaround.

like image 162
Michael Hegner Avatar answered Oct 13 '22 18:10

Michael Hegner