Install with maven
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>logcaptor</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
LogCaptor is a library which will enable you to easily capture logging entries for unit testing purposes.
- Log4j with Apache
- Log4j with Lombok
- Log4j2 with Lombok
- Slf4j with Lombok
- Java Util Logging with Lombok
See the unit test LogCaptorShould for all the scenario's
- Java 8
- Java 11+
To be tested class
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!");
}
}
Unit test:
import static org.assertj.core.api.Assertions.assertThat;
import ch.qos.logback.classic.Level;
import nl.altindag.log.LogCaptor;
import org.junit.Test;
public class FooServiceShould {
@Test
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();
// Option 1 to assert logging entries
assertThat(logCaptor.getLogs(Level.INFO)).containsExactly(expectedInfoMessage);
assertThat(logCaptor.getLogs(Level.WARN)).containsExactly(expectedWarnMessage);
// Option 2 to assert logging entries
assertThat(logCaptor.getLogs("INFO").containsExactly(expectedInfoMessage);
assertThat(logCaptor.getLogs("WARN").containsExactly(expectedWarnMessage);
// Option 3 to assert logging entries
assertThat(logCaptor.getLogs())
.hasSize(2)
.containsExactly(expectedInfoMessage, expectedWarnMessage);
}
}
Class which will log events if specific log level has been set
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!");
}
}
Unit test:
import static org.assertj.core.api.Assertions.assertThat;
import ch.qos.logback.classic.Level;
import nl.altindag.log.LogCaptor;
import org.junit.Test;
public class FooServiceShould {
@Test
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.setLogLevel(Level.INFO);
FooService fooService = new FooService();
fooService.sayHello();
assertThat(logCaptor.getLogs(Level.INFO)).containsExactly(expectedInfoMessage);
assertThat(logCaptor.getLogs(Level.DEBUG))
.doesNotContain(expectedDebugMessage)
.isEmpty();
}
}