ghw
ghw

Reputation: 1

ComparisonFailure - Can't See Physical Difference

I'm running some JUnit tests and I ran into a ComparisonFailure, but I cannot understand why because my actual and expected look exactly alike.

correctline = "   The following  quotation about writing  test programs for  a document" + "\n";

My assertequals statement is:

assertEquals("wrong contents: line", correctline, output.toString());

The error is.

wrong contents: line expected:<...rams for  a document[]

but was:<...rams for a document[

]

Upvotes: 0

Views: 84

Answers (2)

Kkkev
Kkkev

Reputation: 4925

It could be different white space at the end of the line. To be certain, put a breakpoint before the assertion and eyeball the expected and actual strings in the debugger- that will be a lot quicker than trying several guesses and re-running the test each time until you get it right.

Upvotes: 0

rgettman
rgettman

Reputation: 178323

The 3 spaces at the front of correctline appear to be the difference. If they're important, then your test has told you that output.toString() is wrong. If they're not important, then either remove the spaces from correctline or place calls to trim() to make whitespace at the beginning or end not matter:

assertEquals("wrong contents: printer", correctline.trim(),
    output.toString().trim());

Upvotes: 2

Related Questions