Skip to content

Commit

Permalink
Align assertion messages
Browse files Browse the repository at this point in the history
Ensure all assertion messages thrown by Axon Framework in faulty test
scenarios are aligned. As part of this, align the actual and expected
values on separate lines for clarity.

#2755
  • Loading branch information
smcvb committed Jun 16, 2023
1 parent 4b51491 commit dd053ec
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 99 deletions.
Expand Up @@ -1025,7 +1025,7 @@ protected void doAppendEvents(List<? extends EventMessage<?>> events) {
"indicate that a wrong aggregate is being triggered.");
} else if (lastEvent.getSequenceNumber() != event.getSequenceNumber() - 1) {
throw new EventStoreException(format("Unexpected sequence number on stored event. " +
"Expected %s, but got %s.",
"Expected %s, \n but got %s.",
lastEvent.getSequenceNumber() + 1,
event.getSequenceNumber()));
}
Expand Down
122 changes: 72 additions & 50 deletions test/src/main/java/org/axonframework/test/aggregate/Reporter.java
Expand Up @@ -34,7 +34,7 @@
import java.util.stream.Collectors;

/**
* The reporter generates extensive human readable reports of what the expected outcome of a test was, and what the
* The reporter generates extensive human-readable reports of what the expected outcome of a test was, and what the
* actual results were.
*
* @author Allard Buijze
Expand Down Expand Up @@ -77,11 +77,11 @@ public void reportWrongEvent(Collection<?> actualEvents, Collection<?> expectedE
public void reportWrongEvent(Collection<?> actualEvents, StringDescription expectation, Throwable probableCause) {
StringBuilder sb = new StringBuilder(
"The published events do not match the expected events.");
sb.append("Expected :")
sb.append("Expected:")
.append(NEWLINE)
.append(expectation)
.append(NEWLINE)
.append("But got");
.append(" But got");
if (actualEvents.isEmpty()) {
sb.append(" none");
} else {
Expand Down Expand Up @@ -113,14 +113,14 @@ public void reportWrongEvent(Collection<?> actualEvents, StringDescription expec
*/
public void reportWrongEvent(Collection<?> actualEvents, Description expectation, Description mismatch, Throwable probableCause) {
StringBuilder sb = new StringBuilder("The published events do not match the expected events.");
sb.append("Expected :");
sb.append(NEWLINE);
sb.append(expectation);
sb.append(NEWLINE);
sb.append("Did not match:");
sb.append(NEWLINE);
sb.append(mismatch);
sb.append(NEWLINE);
sb.append("Expected <")
.append(expectation)
.append(">,")
.append(NEWLINE)
.append(" but got <")
.append(mismatch)
.append(">.")
.append(NEWLINE);
sb.append("Actual Sequence of events:");
if (actualEvents.isEmpty()) {
sb.append(" no events emitted");
Expand Down Expand Up @@ -148,11 +148,15 @@ public void reportUnexpectedException(Throwable actualException, Description exp
StringBuilder sb = new StringBuilder("The command handler threw an unexpected exception");
sb.append(NEWLINE)
.append(NEWLINE)
.append("Expected <") //NOSONAR
.append("Expected <")
.append(expectation.toString())
.append("> but got <exception of type [")
.append(">,")
.append(NEWLINE)
.append(" but got <exception of type [")
.append(actualException.getClass().getSimpleName())
.append("]>. Stack trace follows:")
.append("]>.")
.append(NEWLINE)
.append("Stack trace follows:")
.append(NEWLINE);
writeStackTrace(actualException, sb);
sb.append(NEWLINE);
Expand All @@ -169,11 +173,13 @@ public void reportWrongResult(Object actual, Object expectation) {
StringBuilder sb = new StringBuilder("The command handler returned an unexpected value");
sb.append(NEWLINE)
.append(NEWLINE)
.append("Expected <"); //NOSONAR
sb.append(expectation.toString());
sb.append("> but got <");
.append("Expected <")
.append(expectation.toString())
.append(">,")
.append(NEWLINE)
.append(" but got <");
describe(actual, sb);
sb.append(">")
sb.append(">.")
.append(NEWLINE);
throw new AxonAssertionError(sb.toString());
}
Expand All @@ -188,11 +194,13 @@ public void reportUnexpectedReturnValue(Object actualReturnValue, Description de
StringBuilder sb = new StringBuilder("The command handler returned normally, but an exception was expected");
sb.append(NEWLINE)
.append(NEWLINE)
.append("Expected <"); //NOSONAR
sb.append(description.toString());
sb.append("> but returned with <");
.append("Expected <")
.append(description.toString())
.append(">,")
.append(NEWLINE)
.append(" but got <");
describe(actualReturnValue, sb);
sb.append(">")
sb.append(">.")
.append(NEWLINE);
throw new AxonAssertionError(sb.toString());
}
Expand All @@ -207,11 +215,15 @@ public void reportWrongException(Throwable actualException, Description descript
StringBuilder sb = new StringBuilder("The command handler threw an exception, but not of the expected type")
.append(NEWLINE)
.append(NEWLINE)
.append("Expected <") //NOSONAR
.append("Expected <")
.append(description.toString())
.append("> but got <exception of type [")
.append(">,")
.append(NEWLINE)
.append(" but got <exception of type [")
.append(actualException.getClass().getSimpleName())
.append("]>. Stacktrace follows: ")
.append("]>.")
.append(NEWLINE)
.append("Stack trace follows:")
.append(NEWLINE);
writeStackTrace(actualException, sb);
sb.append(NEWLINE);
Expand All @@ -226,15 +238,17 @@ public void reportWrongException(Throwable actualException, Description descript
*/
public void reportWrongExceptionMessage(Throwable actualException, Description description) {
throw new AxonAssertionError("The command handler threw an exception, but not with expected message"
+ NEWLINE +
NEWLINE +
"Expected <" + //NOSONAR
description.toString() +
"> but got <message [" +
actualException.getMessage() +
"]>." +
NEWLINE +
NEWLINE);
+ NEWLINE
+ NEWLINE
+ "Expected <"
+ description.toString()
+ ">, "
+ NEWLINE
+ " but got <message ["
+ actualException.getMessage()
+ "]>."
+ NEWLINE
+ NEWLINE);
}

/**
Expand All @@ -245,15 +259,17 @@ public void reportWrongExceptionMessage(Throwable actualException, Description d
*/
public void reportWrongExceptionDetails(Object details, Description description) {
throw new AxonAssertionError("The command handler threw an exception, but not with expected details"
+ NEWLINE +
NEWLINE +
"Expected <" + //NOSONAR
description.toString() +
"> but got <details [" +
details +
"]>." +
NEWLINE +
NEWLINE);
+ NEWLINE
+ NEWLINE
+ "Expected <"
+ description.toString()
+ ">,"
+ NEWLINE
+ " but got <details ["
+ details
+ "]>."
+ NEWLINE
+ NEWLINE);
}

/**
Expand All @@ -265,15 +281,19 @@ public void reportWrongExceptionDetails(Object details, Description description)
*/
public void reportDifferentPayloads(Class<?> messageType, Object actual, Object expected) {
throw new AxonAssertionError("One of the messages contained a different payload than expected"
+ NEWLINE + NEWLINE
+ NEWLINE
+ NEWLINE
+ "The message of type [" + messageType.getSimpleName() + "] "
+ "was not as expected."
+ NEWLINE
+ "Expected <" //NOSONAR
+ "Expected <"
+ nullSafeToString(expected)
+ "> but got <"
+ ">,"
+ NEWLINE
+ " but got <"
+ nullSafeToString(actual)
+ ">"
+ ">."
+ NEWLINE
+ NEWLINE);
}

Expand Down Expand Up @@ -302,11 +322,13 @@ public void reportDifferentPayloads(Class<?> messageType, Field field, Object ac

sb.append("was not as expected.")
.append(NEWLINE)
.append("Expected <") //NOSONAR
.append("Expected <")
.append(nullSafeToString(expected))
.append("> but got <")
.append(">,")
.append(NEWLINE)
.append(" but got <")
.append(nullSafeToString(actual))
.append(">")
.append(">.")
.append(NEWLINE);
throw new AxonAssertionError(sb.toString());
}
Expand Down
Expand Up @@ -98,7 +98,8 @@ public void assertScheduledDeadlineMatching(Instant scheduledTime, Matcher<?> ma
matcher.describeTo(expected);
describe(scheduledDeadlines, actual);
throw new AxonAssertionError(format(
"Did not find a deadline at the given deadline manager. \nExpected:\n<%s> at <%s>\nGot:%s\n",
"Did not find a deadline at the given deadline manager.\n"
+ "Expected <%s> at <%s>,\n but got <%s>.\n",
expected, scheduledTime, actual
));
}
Expand All @@ -114,7 +115,7 @@ public void assertNoScheduledDeadlineMatching(Matcher<?> matcher) {
Description unexpected = new StringDescription();
matcher.describeTo(unexpected);
throw new AxonAssertionError(format(
"Unexpected matching deadline found at the given deadline manager. \nGot:%s\n",
"Unexpected matching deadline found at the given deadline manager.\nGot <%s>.\n",
unexpected
));
}
Expand Down Expand Up @@ -148,7 +149,8 @@ public void assertTriggeredDeadlinesMatching(Matcher<? extends Iterable<?>> matc
matcher.describeTo(expected);
describe(triggeredDeadlines, actual);
throw new AxonAssertionError(format(
"Expected deadlines were not triggered at the given deadline manager. \nExpected:\n<%s>\nGot:%s\n",
"Expected deadlines were not triggered at the given deadline manager.\n"
+ "Expected <%s>,\n but got <%s>.\n",
expected, actual
));
}
Expand Down Expand Up @@ -233,9 +235,10 @@ private void assertNumberOfAndMatchTriggeredDeadlines(int numberOfExpectedDeadli
Matcher<? extends Iterable<?>> deadlinesMatcher) {
List<ScheduledDeadlineInfo> triggeredDeadlines = deadlineManager.getTriggeredDeadlines();
if (triggeredDeadlines.size() != numberOfExpectedDeadlines) {
throw new AxonAssertionError(format("Got wrong number of triggered deadlines. Expected <%s>, got <%s>",
numberOfExpectedDeadlines,
triggeredDeadlines.size()));
throw new AxonAssertionError(format(
"Got wrong number of triggered deadlines.\nExpected <%s>,\n but got <%s>.",
numberOfExpectedDeadlines, triggeredDeadlines.size()
));
}
assertTriggeredDeadlinesMatching(deadlinesMatcher);
}
Expand All @@ -246,7 +249,9 @@ private void assertNumberOfAndMatchTriggeredDeadlines(int numberOfExpectedDeadli
public void assertNoScheduledDeadlines() {
List<ScheduledDeadlineInfo> scheduledDeadlines = deadlineManager.getScheduledDeadlines();
if (scheduledDeadlines != null && !scheduledDeadlines.isEmpty()) {
throw new AxonAssertionError("Expected no scheduled deadlines, got " + scheduledDeadlines.size());
throw new AxonAssertionError(
"Expected no scheduled deadlines, but got " + scheduledDeadlines.size() + " deadlines."
);
}
}

Expand Down
Expand Up @@ -70,9 +70,9 @@ public void assertDispatchedEqualTo(Object... expected) {
List<CommandMessage<?>> actual = commandBus.getDispatchedCommands();
if (actual.size() != expected.length) {
throw new AxonAssertionError(format(
"Got wrong number of commands dispatched. Expected <%s>, got <%s>",
expected.length,
actual.size()));
"Got wrong number of commands dispatched.\nExpected <%s>,\n but got <%s>.",
expected.length, actual.size()
));
}
Iterator<CommandMessage<?>> actualIterator = actual.iterator();
Iterator<Object> expectedIterator = Arrays.asList(expected).iterator();
Expand All @@ -85,18 +85,17 @@ public void assertDispatchedEqualTo(Object... expected) {
CommandMessage<?> expectedMessage = (CommandMessage<?>) expectedItem;
if (!expectedMessage.getPayloadType().equals(actualItem.getPayloadType())) {
throw new AxonAssertionError(format(
"Unexpected payload type of command at position %s (0-based). Expected <%s>, got <%s>",
counter,
expectedMessage.getPayloadType(),
actualItem.getPayloadType()));
"Unexpected payload type of command at position %s (0-based).\n"
+ "Expected <%s>,\n but got <%s>.",
counter, expectedMessage.getPayloadType(), actualItem.getPayloadType()
));
}
assertCommandEquality(counter, expectedMessage.getPayload(), actualItem.getPayload());
if (!expectedMessage.getMetaData().equals(actualItem.getMetaData())) {
throw new AxonAssertionError(format(
"Unexpected Meta Data of command at position %s (0-based). Expected <%s>, got <%s>",
counter,
expectedMessage.getMetaData(),
actualItem.getMetaData()));
"Unexpected Meta Data of command at position %s (0-based).\nExpected <%s>,\n but got <%s>.",
counter, expectedMessage.getMetaData(), actualItem.getMetaData()
));
}
} else {
assertCommandEquality(counter, expectedItem, actualItem.getPayload());
Expand All @@ -116,24 +115,23 @@ public void assertDispatchedMatching(Matcher<?> matcher) {
Description actualDescription = new StringDescription();
matcher.describeTo(expectedDescription);
describe(commandBus.getDispatchedCommands(), actualDescription);
throw new AxonAssertionError(format("Incorrect dispatched command. Expected <%s>, but got <%s>",
throw new AxonAssertionError(format("Incorrect dispatched command.\nExpected <%s>,\n but got <%s>.",
expectedDescription, actualDescription));
}
}

private void assertCommandEquality(int commandIndex, Object expected, Object actual) {
if (!expected.equals(actual)) {
if (!expected.getClass().equals(actual.getClass())) {
throw new AxonAssertionError(format("Wrong command type at index %s (0-based). "
+ "Expected <%s>, but got <%s>",
commandIndex,
expected.getClass().getSimpleName(),
actual.getClass().getSimpleName()));
throw new AxonAssertionError(format(
"Wrong command type at index %s (0-based).\nExpected <%s>,\n but got <%s>.",
commandIndex, expected.getClass().getSimpleName(), actual.getClass().getSimpleName()
));
}
Matcher<Object> matcher = Matchers.deepEquals(expected, fieldFilter);
if (!matcher.matches(actual)) {
throw new AxonAssertionError(format(
"Unexpected command at index %s (0-based). Expected <%s>, but got <%s>",
"Unexpected command at index %s (0-based).\nExpected <%s>,\n but got <%s>.",
commandIndex, expected, actual
));
}
Expand Down

0 comments on commit dd053ec

Please sign in to comment.