jackson-databind: `ObjectMapperTest.testCopyWith()` failing on `2.16` branch on system with non-unix system linefeed

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When I try to build on the 2.16 branch using mvn clean verify, I get:

[ERROR] Failures: 
[ERROR]   ObjectMapperTest.testCopyWith:206 expected:<{[
  "color" : "Black",
  "free" : "true",
  "pages" : "204.04"]
}> but was:<{[
  "color" : "Black",
  "free" : "true",
] "pages" : "204.04"
}>

Glancing at the source code, I see it’s comparing against a string with embedded \n (LF) literals.

String readResultAsString = "{\n  \"color\" : \"Black\",\n  \"free\" : \"true\",\n  \"pages\" : \"204.04\"\n}";
assertEquals(readResultAsString, mapper.writeValueAsString(readResult));

Just on the face of it (assuming normally the developers wouldn’t knowingly commit breaking code), I would bet you a cookie that Jackson is producing output using the system-configured line ending (which may in fact be appropriate), yet the person who wrote the test assumed that Jackson is producing LF line endings. The test passed for the developer because they are using a *nix machine. In my case, I’m on a Windows machine so perhaps it produced CRLF endings. (I’m just guessing by glancing at the code.)

If that is indeed the problem, you’ll want to either use System.lineSeparator() when constructing the expected output; or (perhaps a better approach) create a separate generalized output comparison method that normalizes line endings to be used across all the tests (except the few methods, if any, specifically checking for the line endings).

Version Information

No response

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here –>
// Your code here

Expected behavior

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 19 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Would be great to have sanity check tests for CI on Windows system; I assume Github has runners for this but I have not done it before.

+1 on Windows runner. There is windows machine that can be configured via runs-on: windows-latest. If no one started, by the time I get off work, will try to work on it 👍🏼

In that case I feel it is premature to update the title to reflect a cause we have not verified, and that is based only on my educated guess after glancing at the code for literally five seconds or so. Currently the title is accurate based upon our knowledge: there is a certain test that breaks under certain circumstances on the 2.16 branch.

Note that if you go the route of using the default system line endings in the string being compared, there are 100 ways to do it, but one trick is to use a variation of String.format(…) and embed %n. Then formatting will convert the %n to the line ending configured for the system.