Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sterlp/log4j-test-logger

Simple in memory log4j2 appender to assert log messages in JUnit tests
https://github.com/sterlp/log4j-test-logger

appender assert junit junit5 log log4j2 logging logs message test testing

Last synced: 13 days ago
JSON representation

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

Awesome Lists containing this project

README

        

[![Java CI with Maven](https://github.com/sterlp/log4j-test-logger/actions/workflows/maven.yml/badge.svg)](https://github.com/sterlp/log4j-test-logger/actions/workflows/maven.yml)

# 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

```java
@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

```xml

org.sterl.test.log4j
log4j-test-logger
0.1.0
test

```

## Configuration log4j2.xml

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

```xml










```