question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement and use `Matcher#describeMismatch` throughout `axon-test` module

See original GitHub issue

When using the Hamcrest Matcher based validation logic in ResultValidator, like expectEventsMatching(Matcher<? extends List<? super EventMessage<?>>> matcher), it is hard to see the actual mismatch, if something goes wrong.

This is due to the following implementation:

https://github.com/AxonFramework/AxonFramework/blob/e567af75fb44be19006533ba2aac1d0db614edd8/test/src/main/java/org/axonframework/test/aggregate/ResultValidatorImpl.java#L118-L129

It would be better to request the Matcher to describe the mismatch using the describeMismatch(Object item, Description description) method. This will result in a message that highlights the actual mismatch, not the whole matching logic.

I would suggest to change the implementation to:

    public ResultValidator<T> expectEventsMatching(Matcher<? extends List<? super EventMessage<?>>> matcher) {
        if (!matcher.matches(publishedEvents)) {
            final StringDescription description = descriptionOf(matcher);
            description.appendText("\nMismatch:\n");
            matcher.describeMismatch(item, description);
            reporter.reportWrongEvent(publishedEvents, descriptionOf(publishedEvents, matcher), actualException);
        }
        return this;
    }

    private StringDescription descriptionOf(Object item, Matcher<?> matcher) {
        StringDescription description = new StringDescription();
        matcher.describeMismatch(item, description);
        return description;
    }

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fernanfscommented, Oct 1, 2022

I’ve created a PR, @smcvb. In this, I implemented the usage of the describeMismatch mechanic of Hamcrest Matchers. All changes should be backwards compatible. In the Reporter class, I’ve added a new method, that will receive the mismatch in addition to the description. The old method is not used anywhere in the codebase, thus I’ve deprected that method. Thus, it should be possible to accept the PR and still maintain backwards compatibility.

1reaction
fernanfscommented, Sep 29, 2022

Thank you for your reply, @smcvb! I’m absolutely aware, that the number of changes to be made, are quite substantial, to have it consistent. Especially when trying to main a backwards compatible API within the custom matcher implementations. I’m currently giving that a shot, but do not count on me delivering a PR for the whole story anytime soon.

But what I can provide is a pull request that will only address the expectEventsMatching implementation by adding the output of describeMismatch as an additional information. This would not affect the backwards compatibility. For the Matcher-Implementations provided by axon, this still wouldn’t yield any substantial improvements. But when using Matchers implemented by Hamcrest (or custom ones) that support the describeMismatch, this would massively improve the output quality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

org.hamcrest.Matcher.describeMismatch" when running test in ...
With this 4.13 junit library and hamcrest, it uses hamcrest.MatcherAssert when asserting and throws exception- enter image description here.
Read more >
Using Hamcrest for testing - Tutorial - Vogella.com
To use Hamcrest matchers in your test you use the assertThat statement followed by one or several matchers. The usage of Hamcrest matchers...
Read more >
Mixing JUnit, Hamcrest and Mockito: Explaining java.lang ...
Matcher present on the classpath to which org.junit.Assert.assertThat delegates. If you run this from Eclipse or IntelliJ, there's a high chance ...
Read more >
org.hamcrest.Matcher.describeMismatch" when running test in ...
I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I've created a custom matcher that looks like the following: public static class MyMatcher extends ...
Read more >
Blog - Think Code AB
The setup is version controlled and thus traceable. Here is an example where I use this this tool chain: Jenkins; Docker; Java; Maven;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found