Unit Testing Log Messages Made Easy
Unit testing presents specific challenges around logging. A developer and DZone Core members discusses an open source project he created to help.
Join the DZone community and get the full member experience.
Join For FreeAs Java developers, we need to cover a lot of scenarios to ensure the quality of our software and catch bugs as soon as possible when introducing new code. For 99% of all my use cases, AssertJ, JUnit, Mockito, and Wiremock are sufficient enough to cover the test cases. But for the other use cases, like unit testing info, debugging or warning log messages, these frameworks don't help you out. There is also no other framework that can provide an easy to use method to capture log messages.
The answer which the community provided works well, but it needs a lot of boilerplate code to just assert your log events. So, I wanted to make it easier for myself and share it with you! This is how the LogCaptor library came into life.
Include the following dependency in your project as a test dependency:
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>logcaptor</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
Or get here the latest version: Maven Central
Let's assume you have the following service which creates logs:
xxxxxxxxxx
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class FooService {
private static final Logger LOGGER = LogManager.getLogger(FooService.class);
public void sayHello() {
LOGGER.info("Keyboard not responding. Press any key to continue...");
LOGGER.warn("Congratulations, you are pregnant!");
}
}
Or the same service but with the SLF4J API logging facade:
x
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FooService {
private static final Logger LOGGER = LoggerFactory.getLogger(ZooService.class);
public void sayHello() {
LOGGER.info("Keyboard not responding. Press any key to continue...");
LOGGER.warn("Congratulations, you are pregnant!");
}
}
Let's create a unit test for this service to assert the log messages and the log levels:
x
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class FooServiceShould {
public void logInfoAndWarnMessages() {
String expectedInfoMessage = "Keyboard not responding. Press any key to continue...";
String expectedWarnMessage = "Congratulations, you are pregnant!";
LogCaptor<FooService> logCaptor = LogCaptor.forClass(FooService.class);
FooService fooService = new FooService();
fooService.sayHello();
assertThat(logCaptor.getInfoLogs().containsExactly(expectedInfoMessage);
assertThat(logCaptor.getWarnLogs.containsExactly(expectedWarnMessage);
}
}
You can also get all the log messages by discarding the log level:
xxxxxxxxxx
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class FooServiceShould {
public void logInfoAndWarnMessages() {
String expectedInfoMessage = "Keyboard not responding. Press any key to continue...";
String expectedWarnMessage = "Congratulations, you are pregnant!";
LogCaptor<FooService> logCaptor = LogCaptor.forClass(FooService.class);
FooService fooService = new FooService();
fooService.sayHello();
assertThat(logCaptor.getLogs())
.hasSize(2)
.containsExactly(expectedInfoMessage, expectedWarnMessage);
}
}
It is also possible to test certain log messages when the log level is enabled for a specific level; see the example snippet below:
xxxxxxxxxx
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class FooService {
private static final Logger LOGGER = LogManager.getLogger(FooService.class);
public void sayHello() {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Keyboard not responding. Press any key to continue...");
}
LOGGER.info("Congratulations, you are pregnant!");
}
}
In the the example unit test below, having only the info level enabled should log your debug messages:
xxxxxxxxxx
import static org.assertj.core.api.Assertions.assertThat;
import nl.altindag.log.LogCaptor;
import org.junit.jupiter.api.Test;
public class FooServiceShould {
public void logInfoAndWarnMessages() {
String expectedInfoMessage = "Congratulations, you are pregnant!";
String expectedDebugMessage = "Keyboard not responding. Press any key to continue...";
LogCaptor<FooService> logCaptor = LogCaptor.forClass(FooService.class);
logCaptor.setLogLevelToInfo();
FooService fooService = new FooService();
fooService.sayHello();
assertThat(logCaptor.getInfoLogs()).containsExactly(expectedInfoMessage);
assertThat(logCaptor.getDebugLogs())
.doesNotContain(expectedDebugMessage)
.isEmpty();
}
}
The source code is available on GitHub with other examples: GitHub — Log Captor.
Get the latest version from Maven Central.
It also works with Lombok logging annotations, such as Log4j, Log4j2, Slf4j, and Log.
Good luck and enjoy asserting your log messages!
Further Reading
Opinions expressed by DZone contributors are their own.
Comments