Skip to content

Commit

Permalink
Added static helper method and method overloads to make assertions us…
Browse files Browse the repository at this point in the history
…ing the PredicateBuilder more fluent.
  • Loading branch information
topbadger committed Mar 13, 2023
1 parent 2ca1e0f commit affee72
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/github/valfirst/slf4jtest/TestLoggerAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public TestLoggerAssert hasLogged(Predicate<LoggingEvent> predicate) {
return hasLogged(predicate, "Failed to find log matching predicate");
}

/**
* Uses the supplied {@link PredicateBuilder} to construct the predicate with which to Verify that
* a matching {@link LoggingEvent} has been logged by the test logger.
*
* @param predicate the {@link PredicateBuilder} to use to construct the test predicate
* @return a {@link TestLoggerAssert} for chaining
*/
public TestLoggerAssert hasLogged(PredicateBuilder predicate) {
return hasLogged(predicate.build());
}

/**
* Verify that a log message, at a specific level, has not been logged by the test logger.
*
Expand Down Expand Up @@ -150,6 +161,22 @@ public TestLoggerAssert hasNotLogged(Predicate<LoggingEvent> predicate) {
return this;
}

/**
* Uses the supplied {@link PredicateBuilder} to construct the predicate with which to Verify that
* a matching {@link LoggingEvent} has _not_ been logged by the test logger.
*
* @param predicate the {@link PredicateBuilder} to use to construct the test predicate
* @return a {@link TestLoggerAssert} for chaining
*/
public TestLoggerAssert hasNotLogged(PredicateBuilder predicate) {
findEvent(predicate.build())
.ifPresent(
loggingEvent ->
failWithMessage("Found %s, even though we expected not to", loggingEvent));

return this;
}

/**
* Convenience method for a {@link LevelAssert} from a provided test logger.
*
Expand Down Expand Up @@ -204,6 +231,10 @@ public static class PredicateBuilder {
private Predicate<LoggingEvent> throwablePredicate = IGNORE_PREDICATE;
private Predicate<LoggingEvent> levelPredicate = IGNORE_PREDICATE;

public static PredicateBuilder aLog() {
return new PredicateBuilder();
}

public PredicateBuilder withLevel(Level level) {
levelPredicate = event -> event.getLevel().equals(level);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.github.valfirst.slf4jtest.LoggingEvent.warn;
import static com.github.valfirst.slf4jtest.TestLoggerAssert.MdcComparator.CONTAINING;
import static com.github.valfirst.slf4jtest.TestLoggerAssert.MdcComparator.IGNORING;
import static com.github.valfirst.slf4jtest.TestLoggerAssert.PredicateBuilder.aLog;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -187,6 +188,28 @@ void failsWhenPredicateDoesNotMatch() {
}
}

@Nested
class UsingPredicateBuilder {

@Test
void passesWhenPredicateMatches() {
eventsStubbing.thenReturn(ImmutableList.of(warn("A message")));
assertThatNoException()
.isThrownBy(() -> loggerAssert.hasLogged(aLog().withMessage("A message")));
}

@Test
void failsWhenPredicateDoesNotMatch() {
LoggingEvent loggingEvent = warn("A different message");
eventsStubbing.thenReturn(ImmutableList.of(loggingEvent));

assertThatThrownBy(() -> loggerAssert.hasLogged(aLog().withMessage("A message")))
.isInstanceOf(AssertionError.class)
.hasMessage(
"Failed to find log matching predicate" + loggerContainedMessage(loggingEvent));
}
}

abstract class TestCase {

abstract TestLoggerAssert performAssert(
Expand Down Expand Up @@ -415,6 +438,27 @@ void failsWhenPredicateMatches() {
}
}

@Nested
class UsingPredicateBuilder {
@Test
void passesWhenPredicateDoesNotMatch() {
eventsStubbing.thenReturn(ImmutableList.of(warn("A message")));

assertThatNoException()
.isThrownBy(() -> loggerAssert.hasNotLogged(aLog().withMessage("Unexpected")));
}

@Test
void failsWhenPredicateMatches() {
LoggingEvent loggingEvent = warn("A message");
eventsStubbing.thenReturn(ImmutableList.of(loggingEvent));

assertThatThrownBy(() -> loggerAssert.hasNotLogged(aLog().withMessage("A message")))
.isInstanceOf(AssertionError.class)
.hasMessage("Found " + loggingEvent + ", even though we expected not to");
}
}

abstract class TestCase {
abstract TestLoggerAssert performAssert(
TestLoggerAssert loggerAssert, Level level, String message, Object... arguments);
Expand Down

0 comments on commit affee72

Please sign in to comment.