Skip to content

Commit

Permalink
Switch to JUnit 5
Browse files Browse the repository at this point in the history
remove support for log4j 1.X
  • Loading branch information
cstuht authored and Michel Zimmer committed Feb 2, 2022
1 parent 48db897 commit 6907000
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 215 deletions.
38 changes: 35 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# assertj-logging - assertj assertions for logging

_assertj-logging_'s intention is to provide an easy way to unit test expected logging for different logging implementations with [JUnit 4](https://junit.org/junit4/) and [AssertJ](https://assertj.github.io/doc/).
_assertj-logging_'s intention is to provide an easy way to unit test expected logging for different logging implementations with [JUnit](https://junit.org/) and [AssertJ](https://assertj.github.io/doc/).

Starting with version 0.5.0 a [JUnit 5 extension](https://junit.org/junit5/docs/current/user-guide/#extensions) is provided and [JUnit 4](https://junit.org/junit4/) is no longer supported.

Versions up to 0.4.X provide a [JUnit 4 `@Rule`](https://github.com/junit-team/junit4/wiki/Rules).

It currently supports
* [log4j 1.2](https://logging.apache.org/log4j/1.2/): _assertj-logging-log4j12_
* [log4j 2.x](https://logging.apache.org/log4j/2.x/): _assertj-logging-log4j_
* [logback](http://logback.qos.ch/): _assertj-logging-logback_

Expand Down Expand Up @@ -35,7 +38,7 @@ _pom.xml_
<dependency>
<groupId>de.neuland-bfi</groupId>
<artifactId>assertj-logging-log4j</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -48,6 +51,35 @@ After execution of the code under test this rule can be fed to a set of AssertJ

## Usage

### JUnit 5

```java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import de.neuland.assertj.logging.ExpectedLogging;

import static de.neuland.assertj.logging.ExpectedLoggingAssertions.assertThat;

public class LoggingSourceTest {
@RegisterExtension
private final ExpectedLogging logging = ExpectedLogging.forSource(LoggingSource.class);

@Test
void shouldCaptureLogging() {
// given
String expectedMessage = "Error Message";

// when
new LoggingSource().doSomethingThatLogsErrorMessage();

// then
assertThat(logging).hasErrorMessage(expectedMessage);
}
}
```

### JUnit 4
```java
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.neuland.assertj.logging;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.List;

Expand Down Expand Up @@ -32,7 +33,7 @@
* After executing the method that is expected to log,
* you can verify this with assertj assertions defined in {@code ExpectedLoggingAssert}
*/
public abstract class GenericExpectedLogging<APPENDER extends LogEventCaptureAppender> extends TestWatcher {
public abstract class GenericExpectedLogging<APPENDER extends LogEventCaptureAppender> implements BeforeEachCallback, AfterEachCallback {
final String loggingSource;
private final ThreadLocal<APPENDER> threadLocalAppender = new ThreadLocal<>();

Expand All @@ -41,15 +42,15 @@ public abstract class GenericExpectedLogging<APPENDER extends LogEventCaptureApp
}

@Override
protected void starting(Description description) {
public void beforeEach(ExtensionContext context) {
APPENDER captureAppender = addCaptureAppender();
assertLoggerLevelIsAtLeastInfo();

threadLocalAppender.set(captureAppender);
}

@Override
protected void finished(final Description description) {
public void afterEach(ExtensionContext context) {
removeCaptureAppender(threadLocalAppender.get());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.neuland.assertj.logging;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static de.neuland.assertj.logging.LogLevel.*;
import static de.neuland.assertj.logging.TestAssertions.assertThat;
Expand All @@ -20,7 +20,7 @@ abstract class GenericExpectedLoggingTest<RULE extends GenericExpectedLogging<?>
abstract void logInfo(String message);

@Test
public void shouldCaptureLogging() {
void shouldCaptureLogging() {
// given
String message = "Error Message";

Expand All @@ -32,7 +32,7 @@ public void shouldCaptureLogging() {
}

@Test
public void shouldHaveErrorMessage() {
void shouldHaveErrorMessage() {
// given
String message = "Error Message";

Expand All @@ -45,7 +45,7 @@ public void shouldHaveErrorMessage() {
}

@Test
public void shouldNotHaveErrorMessage() {
void shouldNotHaveErrorMessage() {
// given
String infoMessage = "Info Message";
String warningMessage = "Warning Message";
Expand All @@ -59,7 +59,7 @@ public void shouldNotHaveErrorMessage() {
}

@Test
public void shouldHaveErrorMessageMatchingRegularExpression() {
void shouldHaveErrorMessageMatchingRegularExpression() {
// given
String message = "Error Message";

Expand All @@ -73,7 +73,7 @@ public void shouldHaveErrorMessageMatchingRegularExpression() {
}

@Test
public void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() {
void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() {
// given
String message = "Error Message";
Throwable throwable = new RuntimeException("Error Cause");
Expand All @@ -88,7 +88,7 @@ public void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() {
}

@Test
public void shouldHaveErrorMessageWithThrowable() {
void shouldHaveErrorMessageWithThrowable() {
// given
String message = "Error Message";
Throwable throwable = new RuntimeException("Error Cause");
Expand All @@ -101,7 +101,7 @@ public void shouldHaveErrorMessageWithThrowable() {
}

@Test
public void shouldHaveWarningMessage() {
void shouldHaveWarningMessage() {
// given
String message = "Warning Message";

Expand All @@ -113,7 +113,7 @@ public void shouldHaveWarningMessage() {
}

@Test
public void shouldNotHaveWarningMessage() {
void shouldNotHaveWarningMessage() {
// given
String infoMessage = "Info Message";
String errorMessage = "Error Message";
Expand All @@ -127,7 +127,7 @@ public void shouldNotHaveWarningMessage() {
}

@Test
public void shouldHaveWarningMessageWithThrowable() {
void shouldHaveWarningMessageWithThrowable() {
// given
String message = "Warning Message";
Throwable throwable = new RuntimeException("Warning Cause");
Expand All @@ -140,7 +140,7 @@ public void shouldHaveWarningMessageWithThrowable() {
}

@Test
public void shouldHaveWarningMessageMatchingRegularExpression() {
void shouldHaveWarningMessageMatchingRegularExpression() {
// given
String message = "Warning Message";

Expand All @@ -154,7 +154,7 @@ public void shouldHaveWarningMessageMatchingRegularExpression() {
}

@Test
public void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() {
void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() {
// given
String message = "Warning Message";
Throwable throwable = new RuntimeException("Warning Cause");
Expand All @@ -169,7 +169,7 @@ public void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() {
}

@Test
public void shouldHaveInfoMessage() {
void shouldHaveInfoMessage() {
// given
String message = "Info Message";

Expand All @@ -181,7 +181,7 @@ public void shouldHaveInfoMessage() {
}

@Test
public void shouldNotHaveInfoMessage() {
void shouldNotHaveInfoMessage() {
// given
String warningMessage = "Warning Message";
String errorMessage = "Error Message";
Expand All @@ -195,7 +195,7 @@ public void shouldNotHaveInfoMessage() {
}

@Test
public void shouldHaveInfoMessageMatchingRegularExpression() {
void shouldHaveInfoMessageMatchingRegularExpression() {
// given
String message = "Info Message";

Expand All @@ -209,7 +209,7 @@ public void shouldHaveInfoMessageMatchingRegularExpression() {
}

@Test
public void shouldContainErrorMessages() {
void shouldContainErrorMessages() {
// given
String message1 = "Error message one";
String message2 = "Error message two";
Expand All @@ -225,7 +225,7 @@ public void shouldContainErrorMessages() {
}

@Test
public void shouldContainWarningMessages() {
void shouldContainWarningMessages() {
// given
String message1 = "Warning message one";
String message2 = "Warning message two";
Expand All @@ -241,7 +241,7 @@ public void shouldContainWarningMessages() {
}

@Test
public void shouldContainInfoMessages() {
void shouldContainInfoMessages() {
// given
String message1 = "Info message one";
String message2 = "Info message two";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package de.neuland.assertj.logging;

import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;


public class Log4jExpectedLoggingTest extends GenericExpectedLoggingTest<ExpectedLogging> {
@Rule
public ExpectedLogging logging = ExpectedLogging.forSource(TestLogSource.class);
@RegisterExtension
private final ExpectedLogging logging = ExpectedLogging.forSource(TestLogSource.class);

private TestLogSource logSource;

@Before
@BeforeEach
public void setup() {
logSource = new TestLogSource();
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 6907000

Please sign in to comment.