Merge pull request #7289 from JonCook/master
BAEL-2912 - TestWatcher API
This commit is contained in:
commit
66c91e7295
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.extensions.testwatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.TestWatcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestResultLoggerExtension implements TestWatcher, AfterAllCallback {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TestResultLoggerExtension.class);
|
||||
|
||||
private List<TestResultStatus> testResultsStatus = new ArrayList<>();
|
||||
|
||||
private enum TestResultStatus {
|
||||
SUCCESSFUL, ABORTED, FAILED, DISABLED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testDisabled(ExtensionContext context, Optional<String> reason) {
|
||||
LOG.info("Test Disabled for test {}: with reason :- {}", context.getDisplayName(), reason.orElse("No reason"));
|
||||
|
||||
testResultsStatus.add(TestResultStatus.DISABLED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSuccessful(ExtensionContext context) {
|
||||
LOG.info("Test Successful for test {}: ", context.getDisplayName());
|
||||
|
||||
testResultsStatus.add(TestResultStatus.SUCCESSFUL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testAborted(ExtensionContext context, Throwable cause) {
|
||||
LOG.info("Test Aborted for test {}: ", context.getDisplayName());
|
||||
|
||||
testResultsStatus.add(TestResultStatus.ABORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testFailed(ExtensionContext context, Throwable cause) {
|
||||
LOG.info("Test Aborted for test {}: ", context.getDisplayName());
|
||||
|
||||
testResultsStatus.add(TestResultStatus.FAILED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) throws Exception {
|
||||
Map<TestResultStatus, Long> summary = testResultsStatus.stream()
|
||||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
||||
|
||||
LOG.info("Test result summary for {} {}", context.getDisplayName(), summary.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.extensions.testwatcher;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
@ExtendWith(TestResultLoggerExtension.class)
|
||||
class TestWatcherAPIUnitTest {
|
||||
|
||||
@Test
|
||||
void givenFalseIsTrue_whenTestAbortedThenCaptureResult() {
|
||||
Assumptions.assumeTrue(false);
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void givenTrueIsTrue_whenTestDisabledThenCaptureResult() {
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTrueIsTrue_whenTestAbortedThenCaptureResult() {
|
||||
Assumptions.assumeTrue(true);
|
||||
}
|
||||
|
||||
@Disabled("This test is disabled")
|
||||
@Test
|
||||
void givenFailure_whenTestDisabledWithReason_ThenCaptureResult() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue