Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing Hamcrest Matcher does not print expected value #820

Open
bastian-schlueter opened this issue Sep 3, 2024 · 1 comment
Open

Failing Hamcrest Matcher does not print expected value #820

bastian-schlueter opened this issue Sep 3, 2024 · 1 comment

Comments

@bastian-schlueter
Copy link

Currently using json-unit-core:3.4.1, facing incomplete output on test failure when using a Hamcrest Matcher as a custom Matcher.

Problem

Given the following assertion:

net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson(
        """
    {
      "someText": "abc123"
    }
    """)
    .withMatcher("exampleMatcher", org.hamcrest.Matchers.equalTo("def456"))
    .isEqualTo(
        """
    {
      "someText": "${json-unit.matches:exampleMatcher}"
    }
    """);

I expected the resulting message to contain the expected value def456.
Instead the output looks like this:

JSON documents are different:
Matcher "exampleMatcher" does not match value "abc123" in node "someText". was "abc123"

Notice the . was "abc123" at the end, which just repeats the actual value again.
This can be highly confusing as there is no information in the message, what the expected value was.

Cause

This happens due to a missing call between these two lines:

Description description = new StringDescription();
matcher.describeMismatch(value, description);

Didn't look too deep into the Matcher interface, but looking at existing Hamcrest Matchers it seems like matcher.describeMismatch(value, description) only populates the description with the actual value.

Possible solution

Calling matcher.describeTo(description) between lines 36 and 37, mentioned above, should allow the Hamcrest Matcher to populate the description with the expected value, although I must admit, the formatting looks a little funky due to a missing space character:

JSON documents are different:
Matcher "exampleMatcher" does not match value "abc123" in node "someText". "def456"was "abc123"
@lukas-krecan
Copy link
Owner

Hi, thanks for report. You are right, this should fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants