From 6907000bc06bc86ed5bec68be50069345c5fb0cb Mon Sep 17 00:00:00 2001 From: Christian Stuht Date: Wed, 2 Feb 2022 15:21:39 +0100 Subject: [PATCH] Switch to JUnit 5 remove support for log4j 1.X --- Readme.md | 38 +++++++++++- .../logging/GenericExpectedLogging.java | 11 ++-- .../logging/GenericExpectedLoggingTest.java | 36 ++++++------ .../logging/Log4jExpectedLoggingTest.java | 10 ++-- .../assertj/logging/ExpectedLogging.java | 46 --------------- .../logging/ExpectedLoggingAssertions.java | 7 --- .../Log4j12LogEventCaptureAppender.java | 58 ------------------- .../logging/Log4j12ExpectedLoggingTest.java | 41 ------------- .../src/test/resources/log4j.xml | 13 ----- .../logging/LogbackExpectedLoggingTest.java | 6 +- build.gradle | 28 +++++---- settings.gradle | 2 +- 12 files changed, 81 insertions(+), 215 deletions(-) delete mode 100644 assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLogging.java delete mode 100644 assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLoggingAssertions.java delete mode 100644 assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/Log4j12LogEventCaptureAppender.java delete mode 100644 assertj-logging-log4j12/src/test/java/de/neuland/assertj/logging/Log4j12ExpectedLoggingTest.java delete mode 100644 assertj-logging-log4j12/src/test/resources/log4j.xml diff --git a/Readme.md b/Readme.md index 77c2f39..a9010fd 100644 --- a/Readme.md +++ b/Readme.md @@ -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_ @@ -35,7 +38,7 @@ _pom.xml_ de.neuland-bfi assertj-logging-log4j - 0.4.0 + 0.5.0 test ``` @@ -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; diff --git a/assertj-logging-core/src/main/java/de/neuland/assertj/logging/GenericExpectedLogging.java b/assertj-logging-core/src/main/java/de/neuland/assertj/logging/GenericExpectedLogging.java index d2c4074..c3697ff 100644 --- a/assertj-logging-core/src/main/java/de/neuland/assertj/logging/GenericExpectedLogging.java +++ b/assertj-logging-core/src/main/java/de/neuland/assertj/logging/GenericExpectedLogging.java @@ -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; @@ -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 extends TestWatcher { +public abstract class GenericExpectedLogging implements BeforeEachCallback, AfterEachCallback { final String loggingSource; private final ThreadLocal threadLocalAppender = new ThreadLocal<>(); @@ -41,7 +42,7 @@ public abstract class GenericExpectedLogging abstract void logInfo(String message); @Test - public void shouldCaptureLogging() { + void shouldCaptureLogging() { // given String message = "Error Message"; @@ -32,7 +32,7 @@ public void shouldCaptureLogging() { } @Test - public void shouldHaveErrorMessage() { + void shouldHaveErrorMessage() { // given String message = "Error Message"; @@ -45,7 +45,7 @@ public void shouldHaveErrorMessage() { } @Test - public void shouldNotHaveErrorMessage() { + void shouldNotHaveErrorMessage() { // given String infoMessage = "Info Message"; String warningMessage = "Warning Message"; @@ -59,7 +59,7 @@ public void shouldNotHaveErrorMessage() { } @Test - public void shouldHaveErrorMessageMatchingRegularExpression() { + void shouldHaveErrorMessageMatchingRegularExpression() { // given String message = "Error Message"; @@ -73,7 +73,7 @@ public void shouldHaveErrorMessageMatchingRegularExpression() { } @Test - public void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() { + void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() { // given String message = "Error Message"; Throwable throwable = new RuntimeException("Error Cause"); @@ -88,7 +88,7 @@ public void shouldHaveErrorMessageMatchingRegularExpressionWithThrowable() { } @Test - public void shouldHaveErrorMessageWithThrowable() { + void shouldHaveErrorMessageWithThrowable() { // given String message = "Error Message"; Throwable throwable = new RuntimeException("Error Cause"); @@ -101,7 +101,7 @@ public void shouldHaveErrorMessageWithThrowable() { } @Test - public void shouldHaveWarningMessage() { + void shouldHaveWarningMessage() { // given String message = "Warning Message"; @@ -113,7 +113,7 @@ public void shouldHaveWarningMessage() { } @Test - public void shouldNotHaveWarningMessage() { + void shouldNotHaveWarningMessage() { // given String infoMessage = "Info Message"; String errorMessage = "Error Message"; @@ -127,7 +127,7 @@ public void shouldNotHaveWarningMessage() { } @Test - public void shouldHaveWarningMessageWithThrowable() { + void shouldHaveWarningMessageWithThrowable() { // given String message = "Warning Message"; Throwable throwable = new RuntimeException("Warning Cause"); @@ -140,7 +140,7 @@ public void shouldHaveWarningMessageWithThrowable() { } @Test - public void shouldHaveWarningMessageMatchingRegularExpression() { + void shouldHaveWarningMessageMatchingRegularExpression() { // given String message = "Warning Message"; @@ -154,7 +154,7 @@ public void shouldHaveWarningMessageMatchingRegularExpression() { } @Test - public void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() { + void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() { // given String message = "Warning Message"; Throwable throwable = new RuntimeException("Warning Cause"); @@ -169,7 +169,7 @@ public void shouldHaveWarningMessageMatchingRegularExpressionWithThrowable() { } @Test - public void shouldHaveInfoMessage() { + void shouldHaveInfoMessage() { // given String message = "Info Message"; @@ -181,7 +181,7 @@ public void shouldHaveInfoMessage() { } @Test - public void shouldNotHaveInfoMessage() { + void shouldNotHaveInfoMessage() { // given String warningMessage = "Warning Message"; String errorMessage = "Error Message"; @@ -195,7 +195,7 @@ public void shouldNotHaveInfoMessage() { } @Test - public void shouldHaveInfoMessageMatchingRegularExpression() { + void shouldHaveInfoMessageMatchingRegularExpression() { // given String message = "Info Message"; @@ -209,7 +209,7 @@ public void shouldHaveInfoMessageMatchingRegularExpression() { } @Test - public void shouldContainErrorMessages() { + void shouldContainErrorMessages() { // given String message1 = "Error message one"; String message2 = "Error message two"; @@ -225,7 +225,7 @@ public void shouldContainErrorMessages() { } @Test - public void shouldContainWarningMessages() { + void shouldContainWarningMessages() { // given String message1 = "Warning message one"; String message2 = "Warning message two"; @@ -241,7 +241,7 @@ public void shouldContainWarningMessages() { } @Test - public void shouldContainInfoMessages() { + void shouldContainInfoMessages() { // given String message1 = "Info message one"; String message2 = "Info message two"; diff --git a/assertj-logging-log4j/src/test/java/de/neuland/assertj/logging/Log4jExpectedLoggingTest.java b/assertj-logging-log4j/src/test/java/de/neuland/assertj/logging/Log4jExpectedLoggingTest.java index 58e73e7..1bbdd27 100644 --- a/assertj-logging-log4j/src/test/java/de/neuland/assertj/logging/Log4jExpectedLoggingTest.java +++ b/assertj-logging-log4j/src/test/java/de/neuland/assertj/logging/Log4jExpectedLoggingTest.java @@ -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 { - @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(); } diff --git a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLogging.java b/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLogging.java deleted file mode 100644 index e60c44f..0000000 --- a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLogging.java +++ /dev/null @@ -1,46 +0,0 @@ -package de.neuland.assertj.logging; - -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; - - -/** - * {@inheritDoc} - */ -public class ExpectedLogging extends GenericExpectedLogging { - private final Logger logger; - private ExpectedLogging(String loggingSource) { - super(loggingSource); - this.logger = getLogger(); - } - - public static ExpectedLogging forSource(Class loggingSource) { - return new ExpectedLogging(loggingSource.getCanonicalName()); - } - - @Override - Log4j12LogEventCaptureAppender addCaptureAppender() { - Log4j12LogEventCaptureAppender captureAppender = new Log4j12LogEventCaptureAppender(); - logger.addAppender(captureAppender); - - return captureAppender; - } - - @Override - void assertLoggerLevelIsAtLeastInfo() { - final Level loggerLevel = logger.getLevel(); - if (loggerLevel == null || loggerLevel.isGreaterOrEqual(Level.INFO)) { - logger.setLevel(Level.INFO); - } - } - - @Override - void removeCaptureAppender(final Log4j12LogEventCaptureAppender appender) { - logger.removeAppender(appender); - } - - private Logger getLogger() { - return LogManager.getLogger(loggingSource); - } -} diff --git a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLoggingAssertions.java b/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLoggingAssertions.java deleted file mode 100644 index 77c2ad2..0000000 --- a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/ExpectedLoggingAssertions.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.neuland.assertj.logging; - -public class ExpectedLoggingAssertions { - public static ExpectedLoggingAssert assertThat(ExpectedLogging actual) { - return new ExpectedLoggingAssert(actual); - } -} diff --git a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/Log4j12LogEventCaptureAppender.java b/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/Log4j12LogEventCaptureAppender.java deleted file mode 100644 index 9ac4ade..0000000 --- a/assertj-logging-log4j12/src/main/java/de/neuland/assertj/logging/Log4j12LogEventCaptureAppender.java +++ /dev/null @@ -1,58 +0,0 @@ -package de.neuland.assertj.logging; - -import org.apache.log4j.AppenderSkeleton; -import org.apache.log4j.Level; -import org.apache.log4j.spi.LoggingEvent; - -import java.util.ArrayList; -import java.util.List; - - -public class Log4j12LogEventCaptureAppender extends AppenderSkeleton implements LogEventCaptureAppender { - private final List logEvents = new ArrayList<>(); - - @Override - public List getLogEvents() { - return logEvents; - } - - @Override - public void append(LoggingEvent event) { - logEvents.add(new LogEvent(logLevel(event), - message(event), - throwable(event))); - } - - @Override - public void close() { - } - - @Override - public boolean requiresLayout() { - return false; - } - - private LogLevel logLevel(LoggingEvent event) { - Level level = event.getLevel(); - if (Level.ERROR.equals(level)) { - return LogLevel.ERROR; - } - if (Level.WARN.equals(level)) { - return LogLevel.WARNING; - } - if (Level.INFO.equals(level)) { - return LogLevel.INFO; - } - return LogLevel.IGNORED; - } - - private String message(LoggingEvent event) { - return event.getRenderedMessage(); - } - - private Throwable throwable(LoggingEvent event) { - return event.getThrowableInformation() != null - ? event.getThrowableInformation().getThrowable() - : null; - } -} diff --git a/assertj-logging-log4j12/src/test/java/de/neuland/assertj/logging/Log4j12ExpectedLoggingTest.java b/assertj-logging-log4j12/src/test/java/de/neuland/assertj/logging/Log4j12ExpectedLoggingTest.java deleted file mode 100644 index e90fb7e..0000000 --- a/assertj-logging-log4j12/src/test/java/de/neuland/assertj/logging/Log4j12ExpectedLoggingTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package de.neuland.assertj.logging; - -import lombok.extern.log4j.Log4j; -import org.junit.Rule; - - -@Log4j -public class Log4j12ExpectedLoggingTest extends GenericExpectedLoggingTest { - @Rule - public ExpectedLogging logging = ExpectedLogging.forSource(Log4j12ExpectedLoggingTest.class); - - @Override - ExpectedLogging expectedLoggingRule() { - return logging; - } - - @Override - void logError(String message, Throwable detail) { - LOG.error(message, detail); - } - - @Override - void logError(String message) { - LOG.error(message); - } - - @Override - void logWarning(String message, Throwable detail) { - LOG.warn(message, detail); - } - - @Override - void logWarning(String message) { - LOG.warn(message); - } - - @Override - void logInfo(String message) { - LOG.info(message); - } -} diff --git a/assertj-logging-log4j12/src/test/resources/log4j.xml b/assertj-logging-log4j12/src/test/resources/log4j.xml deleted file mode 100644 index 35f75bd..0000000 --- a/assertj-logging-log4j12/src/test/resources/log4j.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/assertj-logging-logback/src/test/java/de/neuland/assertj/logging/LogbackExpectedLoggingTest.java b/assertj-logging-logback/src/test/java/de/neuland/assertj/logging/LogbackExpectedLoggingTest.java index 5a885f9..89d36f1 100644 --- a/assertj-logging-logback/src/test/java/de/neuland/assertj/logging/LogbackExpectedLoggingTest.java +++ b/assertj-logging-logback/src/test/java/de/neuland/assertj/logging/LogbackExpectedLoggingTest.java @@ -1,14 +1,14 @@ package de.neuland.assertj.logging; import lombok.extern.slf4j.Slf4j; -import org.junit.Rule; +import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; @Slf4j public class LogbackExpectedLoggingTest extends GenericExpectedLoggingTest { - @Rule - public ExpectedLogging logging = ExpectedLogging.forSource(LogbackExpectedLoggingTest.class); + @RegisterExtension + private final ExpectedLogging logging = ExpectedLogging.forSource(LogbackExpectedLoggingTest.class); @Override ExpectedLogging expectedLoggingRule() { diff --git a/build.gradle b/build.gradle index 469beb4..d1718ba 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,10 @@ import javax.naming.ConfigurationException ext { - junitVersion = '4.13.2' + junitVersion = '5.8.2' assertjVersion = '3.22.0' lombokVersion = '1.18.22' - log4jVersion = '1.2.17' - log4j2Version = '2.17.1' + log4jVersion = '2.17.1' logbackVersion = '1.2.10' isReleaseVersion = !version.endsWith('SNAPSHOT') @@ -21,7 +20,7 @@ project(':assertj-logging-core') { subprojects { group = 'de.neuland-bfi' - version = '0.4.0' + version = '0.5.0' repositories { mavenCentral() @@ -37,14 +36,16 @@ subprojects { } dependencies { - implementation "junit:junit:${junitVersion}" - implementation "org.assertj:assertj-core:${assertjVersion}" + api "org.junit.jupiter:junit-jupiter-api:${junitVersion}" + api "org.assertj:assertj-core:${assertjVersion}" compileOnly "org.projectlombok:lombok:${lombokVersion}" annotationProcessor "org.projectlombok:lombok:${lombokVersion}" testCompileOnly "org.projectlombok:lombok:${lombokVersion}" testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}" + + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}" } if (it.name != 'assertj-logging-core') { @@ -60,6 +61,10 @@ subprojects { withJavadocJar() } + test { + useJUnitPlatform() + } + publishing { publications { mavenJava(MavenPublication) { @@ -67,7 +72,7 @@ subprojects { pom { name = 'AssertJ Logging' - description = 'Logging Assertions for JUnit 4 and AssertJ' + description = 'Logging Assertions for JUnit 5 and AssertJ' url = 'https://github.com/neuland/assertj-logging' licenses { license { @@ -135,16 +140,9 @@ subprojects { } -project(':assertj-logging-log4j12') { - dependencies { - implementation "log4j:log4j:${log4jVersion}" - } -} - - project(':assertj-logging-log4j') { dependencies { - implementation "org.apache.logging.log4j:log4j-core:${log4j2Version}" + implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}" } } diff --git a/settings.gradle b/settings.gradle index a208ac8..8b85bbf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = 'assertj-logging' include 'assertj-logging-core' -include 'assertj-logging-log4j', 'assertj-logging-log4j12', 'assertj-logging-logback' +include 'assertj-logging-log4j', 'assertj-logging-logback'