junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.52k stars 3.26k forks source link

Make assertEquals platform independent #1729

Closed liyubin117 closed 2 years ago

liyubin117 commented 2 years ago

we use assertEquals to check the equality of objects in many scenarios, but an annoying issue is that line separator is different in unix and windows, sometimes we encounted the junit failed just only caused by platform dependent, in fact, the problem has been mentioned frequently in Stackoverflow, maybe we can fix it

image image

panchenko commented 2 years ago

@liyubin117 There could be different situations. In some cases there should be an exact match, in some cases the line separators are not important. As this function has been doing the exact comparison for decades - it would be better keeping that behaviour. You can implement the platform independent asserts in your code or even make it an open source library.

liyubin117 commented 2 years ago

@panchenko Thanks for your review. Given many developers have been confused by the issue, and update version of junit dependency is a easier way to fix the issue for users, we can add a function assertEqualsPlatformIndependent to avoid inflecting running projects, also give users an choice to check equality ignore platform difference

kcooney commented 2 years ago

It sounds like the assertion is failing due to the behavior of your code that generates the expected and/or actual values, so JUnit is working as intended.

I agree that if there is a need for an assertion that ignores line endings, that should be done outside of JUnit.

Note: if you make your own version of this, then I suggest that expected and actual be of type String.

kcooney commented 2 years ago

It also looks like your IDE is incorrectly showing no differences.

If the following code fails then the problem isn't JUnit:

if (expected == null) {
  if (actual != null) {
    throw ...
  }
} else if (!expected.toSting().equals(actual.toString())) {
  throw ...
}
kcooney commented 2 years ago

If you use AssertJ you can use this: http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/AbstractCharSequenceAssert.html#isEqualToIgnoringNewLines(java.lang.CharSequence)

I don't see an equivalent in Truth.

liyubin117 commented 2 years ago

@kcooney Thanks for reminding that