Coder Social home page Coder Social logo

sterlp / log4j-test-logger Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 24 KB

Simple in memory log4j2 appender to assert log messages in JUnit tests

License: MIT License

Java 100.00%
junit5 junit log4j2 testing appender assert logs log logging test

log4j-test-logger's Introduction

Java CI with Maven

log4j-test-logger

Simple in memory log4j2 appender to assert logs in JUnit tests.

Alternative if you use Spring

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/system/OutputCaptureExtension.html

Usage

    @BeforeEach
    void before() {
        TestAppender.clear();
    }
    @Test
    void yourTestMethod() {
        // GIVEN: log is clean
        TestAppender.clear();

        // WHEN: I call my fancy method
        subject.doFancyStuff(argument);

        // THEN: I should have
        assertFalse(TestAppender.first(Level.INFO, "Fancy method executed").isPresent());
        assertEquals(1, TestAppender.count(Level.INFO));
        assertEquals(0, TestAppender.count(Level.WARN));     
        assertEquals(0, TestAppender.count(Level.ERROR));
    }

Configuration

Dependency Maven pom.xml

        <dependency>
            <groupId>org.sterl.test.log4j</groupId>
            <artifactId>log4j-test-logger</artifactId>
            <version>0.1.0</version>
            <scope>test</scope>
        </dependency>

Configuration log4j2.xml

In your src/test/resources add or extend the log4j file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!--  define the test appender -->
        <TestAppender name="TestAppender" />

    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />

            <!-- include the appender -->
            <AppenderRef ref="TestAppender" />

        </Root>
    </Loggers>
</Configuration>

log4j-test-logger's People

Contributors

sterlp avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

migoldschmidt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.