Pivoman
Pivoman

Reputation: 4805

org.junit.ComparisonFailure with identical results

I cloned existing project, where are some unit tests in JUnit-4.12. These tests are launched after every push to remote depository and they will end successfuly, but when I run them on my local machine in eclipse, they will end with ComparisonFailure.

org.junit.ComparisonFailure expected:<xy> but was:<xy>

With this error will end every test, where I'm trying to compare sent json with existing json file.

Test:

@Test
public void calculateSignedCasesView() throws JsonProcessingException {
    Case case1 = prepareCase();
    getEm().persist(case1);
    SignedCasesViewFilter filter = new SignedCasesViewFilter(new LinkedHashSet<String>(Arrays.asList("testCode")), new Date(0), new Date(0));
    SignedCasesView view = reportService.calculateSignedCasesView(filter);
    String actual = getObjectMapper().writeValueAsString(view);
    String expected = readToString("signedCasesReportView01.json");
    Assert.assertEquals(expected, actual);
}

When I look on result, there are highlighted initial spaces of every row.

Result:test result

I tried copy this result to MS Word to look, if there aren't any non-breaking spaces (discussed here), but all spaces are ordinary. Or I tried to run tests with different encoding, but nothing works.

Upvotes: 1

Views: 6701

Answers (2)

Ashley Frieze
Ashley Frieze

Reputation: 5458

This may be a line endings issue as Veselin suggested. Perhaps consider a semantic comparison where the exact format of the file is not necessary.

You can achieve this with ModelAssert - https://github.com/webcompere/model-assert

String actual = getObjectMapper().writeValueAsString(view);
String expected = readToString("signedCasesReportView01.json");
assertJson(actual)
    .isEqualTo(expected);

Where they are not the same, this will also output the paths that are different.

Upvotes: 0

Veselin Davidov
Veselin Davidov

Reputation: 7081

If the tests work on the repository and they don't work on your local machine I guess there is some difference in the encoding or in the way you produce the json. You haven't shown for example readToString function or the signedCasesReportView01.json file which may be different on the repository and in your local environment. My guess would be new lines which is the most common problem in different environments and when I look in your test cases there aren't any other special characters.

For example it often happens when you copy files to git repository and it is modified by the config core.autocrlf and it automatically changes \r\n from your local windows machine to \n because it is unix on the other side. And even the commited file might not show difference in the client but they can be actually different. Compare the byte streams or line by line and you should be able to spot the difference.

Upvotes: 2

Related Questions