Skip to content

Captures log entries for unit testing purposes

License

Notifications You must be signed in to change notification settings

hanson76/log-captor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Actions Status Maintainability Codacy Badge codecov Apache2 license Maven Central FOSSA Status

SonarCloud

Install with maven

<dependency>
    <groupId>io.github.hakky54</groupId>
    <artifactId>logcaptor</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

Introduction

LogCaptor is a library which will enable you to easily capture logging entries for unit testing purposes.

Tested Logging libraries

  • 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

Supported Java versions

  • Java 8
  • Java 11+

Usage

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();
    }
}

License

FOSSA Status

About

Captures log entries for unit testing purposes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.1%
  • Shell 0.9%