Skip to content

Commit

Permalink
Introduce AssertionTestUtils.assertMessageMatches
Browse files Browse the repository at this point in the history
assertMessageMatches() is a convenience method for using a RegEx instead
of assertMessageStartsWith()/assertMessageEndsWith() combos.

This commit makes use of the new method in AssertNullAssertionsTests but
leaves all other existing tests unchanged.

assertMessageMatches() can be used for new tests, or we can slowly
migrate to it where it makes sense as we modify existing tests for some
other reason.
  • Loading branch information
sbrannen committed Jan 31, 2021
1 parent 68bcfdd commit ebe7408
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Expand Up @@ -11,8 +11,8 @@
package org.junit.jupiter.api;

import static org.junit.jupiter.api.AssertionTestUtils.assertExpectedAndActualValues;
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEndsWith;
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageMatches;
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand Down Expand Up @@ -46,7 +46,7 @@ void assertNullWithNonNullObject() {
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageEndsWith(ex, "expected: <null> but was: <foo>");
assertMessageEquals(ex, "expected: <null> but was: <foo>");
assertExpectedAndActualValues(ex, null, "foo");
}
}
Expand Down Expand Up @@ -76,8 +76,7 @@ private void assertNullWithNonNullObjectWithNullStringReturnedFromToString(Suppl
// Should look something like:
// expected: <null> but was: java.lang.String@264b3504<null>
String prefix = (messageSupplier != null ? messageSupplier.get() + " ==> " : "");
assertMessageStartsWith(ex, prefix + "expected: <null> but was: java.lang.String@");
assertMessageEndsWith(ex, "<null>");
assertMessageMatches(ex, prefix + "expected: <null> but was: java\\.lang\\.String@.+<null>");
assertExpectedAndActualValues(ex, null, actual);
}
}
Expand Down Expand Up @@ -107,9 +106,8 @@ private void assertNullWithNonNullObjectWithNullReferenceReturnedFromToString(Su
// Should look something like:
// expected: <null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@4e7912d8<null>
String prefix = (messageSupplier != null ? messageSupplier.get() + " ==> " : "");
assertMessageStartsWith(ex,
prefix + "expected: <null> but was: org.junit.jupiter.api.AssertNullAssertionsTests$NullToString@");
assertMessageEndsWith(ex, "<null>");
assertMessageMatches(ex, prefix
+ "expected: <null> but was: org\\.junit\\.jupiter\\.api\\.AssertNullAssertionsTests\\$NullToString@.+<null>");
assertExpectedAndActualValues(ex, null, actual);
}
}
Expand All @@ -121,8 +119,7 @@ void assertNullWithNonNullObjectAndMessage() {
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageStartsWith(ex, "a message");
assertMessageEndsWith(ex, "expected: <null> but was: <foo>");
assertMessageEquals(ex, "a message ==> expected: <null> but was: <foo>");
assertExpectedAndActualValues(ex, null, "foo");
}
}
Expand All @@ -134,8 +131,7 @@ void assertNullWithNonNullObjectAndMessageSupplier() {
expectAssertionFailedError();
}
catch (AssertionFailedError ex) {
assertMessageStartsWith(ex, "test");
assertMessageEndsWith(ex, "expected: <null> but was: <foo>");
assertMessageEquals(ex, "test ==> expected: <null> but was: <foo>");
assertExpectedAndActualValues(ex, null, "foo");
}
}
Expand Down
Expand Up @@ -38,6 +38,13 @@ static void assertMessageEquals(Throwable ex, String msg) throws AssertionError
}
}

static void assertMessageMatches(Throwable ex, String regex) throws AssertionError {
if (!ex.getMessage().matches(regex)) {
throw new AssertionError("Exception message should match regular expression [" + regex + "], but was ["
+ ex.getMessage() + "].");
}
}

static void assertMessageStartsWith(Throwable ex, String msg) throws AssertionError {
if (!ex.getMessage().startsWith(msg)) {
throw new AssertionError(
Expand Down

0 comments on commit ebe7408

Please sign in to comment.