Skip to content

Commit

Permalink
Merge pull request #18 from dm-drogeriemarkt/assertion-message
Browse files Browse the repository at this point in the history
fix wrong and misleading assertion message
  • Loading branch information
cleaning-agent authored Feb 3, 2023
2 parents eb7245d + 9ee7b89 commit 4683ddf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ logCapture.assertLoggedInOrder(
* [Cucumber example](#cucumber-example)
* [Cucumber feature file](#cucumber-feature-file)
* [Changes](#changes)
* [3.6.1](#361)
* [3.6.0](#360)
* [3.5.0](#350)
* [3.4.1](#341)
Expand All @@ -72,7 +73,7 @@ Add log-capture as a test dependency to your project. If you use Maven, add this
<dependency>
<groupId>de.dm.infrastructure</groupId>
<artifactId>log-capture</artifactId>
<version>3.6.0</version>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -308,6 +309,10 @@ And with MDC logging context

## Changes

### 3.6.1

* Fixed a misleading and wrong assertion message. The assertion itself was correct, but the message always said all matchers did not match when only a subset did not match.

### 3.6.0

* Removed ExpectedKeyValue again due to an [API change in Logstash without a workaround](https://github.com/logfellow/logstash-logback-encoder/issues/788)
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/de/dm/infrastructure/logcapture/LogAsserter.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ private static void throwAssertionForPartiallyMatchingLoggedEvent(Optional<Level
StringBuilder assertionMessage = new StringBuilder();

for (LogEventMatcher logEventMatcher : logEventMatchers) {
assertionMessage.append(format("Expected log message has occurred, but never with the expected %s: %s",
logEventMatcher.getMatcherTypeDescription(), getDescriptionForExpectedMessage(level, regex)));
assertionMessage.append(lineSeparator());
assertionMessage.append(logEventMatcher.getNonMatchingErrorMessage(partiallyMatchingLoggedEvent));
assertionMessage.append(lineSeparator());
if (!logEventMatcher.matches(partiallyMatchingLoggedEvent)) {
assertionMessage.append(format("Expected log message has occurred, but never with the expected %s: %s",
logEventMatcher.getMatcherTypeDescription(), getDescriptionForExpectedMessage(level, regex)));
assertionMessage.append(lineSeparator());
assertionMessage.append(logEventMatcher.getNonMatchingErrorMessage(partiallyMatchingLoggedEvent));
assertionMessage.append(lineSeparator());
}
}
throw new AssertionError(assertionMessage.toString());
}
Expand Down
25 changes: 19 additions & 6 deletions src/test/java/com/example/app/ReadableApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,6 @@ void mdcForAllAndSingleMdcFails() {
));

assertThat(assertionError).hasMessage("Expected log message has occurred, but never with the expected MDC value: Level: WARN, Regex: \"bye world\"" +
lineSeparator() + " captured message: \"bye world\"" +
lineSeparator() + " expected MDC key: key" +
lineSeparator() + " expected MDC value: \".*value.*\"" +
lineSeparator() + " captured MDC values:" +
lineSeparator() + " key: \"value\"" +
lineSeparator() + "Expected log message has occurred, but never with the expected MDC value: Level: WARN, Regex: \"bye world\"" +
lineSeparator() + " captured message: \"bye world\"" +
lineSeparator() + " expected MDC key: another_key" +
lineSeparator() + " expected MDC value: \".*another_value.*\"" +
Expand Down Expand Up @@ -685,4 +679,23 @@ void nothingElseLoggedSingleLogMessageFails() {
assertThat(assertionError).hasMessage("There have been other log messages than the asserted ones.");
}
}

@Test
void combinedLogExpectationsOnlyOutputMismatch() {
MDC.put("key", "a value");
log.error("some error", new RuntimeException("an exception that was logged"));
MDC.clear();

AssertionError assertionError = assertThrows(AssertionError.class, () ->
logCapture.assertLogged(
error("some error",
mdc("key", "a value"),
exception().expectedMessageRegex("an exception that was not logged").build())
));

assertThat(assertionError).hasMessage("Expected log message has occurred, but never with the expected Exception: Level: ERROR, Regex: \"some error\"" +
lineSeparator() + " expected exception: message (regex): \"an exception that was not logged\"" +
lineSeparator() + " actual exception: message: \"an exception that was logged\", message: java.lang.RuntimeException" +
lineSeparator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ void mdcForAllAndSingleMdcFails() {

assertThat(assertionError).hasMessage(
"Expected log message has occurred, but never with the expected MDC value: Level: WARN, Regex: \"bye world\"" +
lineSeparator() + " captured message: \"bye world\"" +
lineSeparator() + " expected MDC key: key" +
lineSeparator() + " expected MDC value: \".*value.*\"" +
lineSeparator() + " captured MDC values:" +
lineSeparator() + " key: \"value\"" +
lineSeparator() + "Expected log message has occurred, but never with the expected MDC value: Level: WARN, Regex: \"bye world\"" +
lineSeparator() + " captured message: \"bye world\"" +
lineSeparator() + " expected MDC key: another_key" +
lineSeparator() + " expected MDC value: \".*another_value.*\"" +
Expand Down

0 comments on commit 4683ddf

Please sign in to comment.