-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
When comparing very long strings using Assertions.assertEquals then the failure output does not indicate what exactly was not equal in the expected and actual strings. In other words, it only prints the expected and actual strings but does not provide any user friendly output that makes it easier for user to spot the difference in two strings.
In comparison JUnit4 highlights the difference by wrapping the diff inside square brackets []
Please take a look at below example strings
- Characters 40–49 are the only differences:
- In String A: stuvwxYZab
- In String B: ZXCVBNMASD
- Everything else is identical
String expected = "ABCDEF1234ghijklMNOPQR56789stuvwxYZabcdefghijGHIJKLmnopqrSTUVWXYZ0123456789abcdefghij";
String actual = "ABCDEF1234ghijklMNOPQR56789ZXCVBNMASDabcdefghijGHIJKLmnopqrSTUVWXYZ0123456789abcdefghij";JUnit4 Output Assert.assertEquals(expected,actual)
org.junit.ComparisonFailure: expected:<...234ghijklMNOPQR56789[stuvwxYZ]abcdefghijGHIJKLmnop...> but was:<...234ghijklMNOPQR56789[ZXCVBNMASD]abcdefghijGHIJKLmnop...>JUnit5 Output Assertions.assertEquals(expected,actual)
org.opentest4j.AssertionFailedError: expected: <ABCDEF1234ghijklMNOPQR56789stuvwxYZabcdefghijGHIJKLmnopqrSTUVWXYZ0123456789abcdefghij> but was: <ABCDEF1234ghijklMNOPQR56789ZXCVBNMASDabcdefghijGHIJKLmnopqrSTUVWXYZ0123456789abcdefghij>Deliverables
JUnit5 should also provide some kind of distinguishing mechanism that makes it easier for user to spot the difference in long texts. It could initially only be limited to String objects so the error message can be formatted to Strings. This will make it easier for users to identify the error without having to use other tools to find a difference in strings, or having to write their own custom utilities. The simplest approach I could think of is to have the same output as JUnit4 with square brackets around the differeing part. In case of multiple differences, it can surround the square brackets at all the places, or it could just print all the differing strings alongwith indexes separately and extend the original error message.
Additional Information
I found a similar opened issue #3192 and #3139 , but they looks like a stale with no progress.