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

NoSuchMethodError asserting in JUnit Tests #145

Closed
pioto opened this Issue Nov 27, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@pioto

pioto commented Nov 27, 2018

When attempting to use isSimilarTo or hasXPath, I get this exception:

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
	at org.junit.Assert.assertThat(Assert.java:956)
        ...

This is with the default versions of JUnit, XMLUnit shipped with Spring Boot 2.0.6.RELEASE:

  • XMLUnit (core & matchers) 2.5.1
  • JUnit 4.12
  • assertj-core 3.9.1
  • hamcrest-core, hamcrest-library 1.3
@krystiankaluzny

This comment has been minimized.

Member

krystiankaluzny commented Nov 27, 2018

@pioto I can't reproduce the issue. I wrote a test that use isSimilarTo but everything works fine.
Look at https://github.com/krystiankaluzny/springboot-xmlunit-test maybe I have wrong dependancy settup or something.
If you create PR that reproduce the issue, I whould be gratefull.

@krystiankaluzny krystiankaluzny added the bug label Nov 27, 2018

@pioto

This comment has been minimized.

pioto commented Nov 28, 2018

Thanks for looking into it. I'll try to come up with a minimal test case for that repo tomorrow, but I think it may be something as simple as:

    @Test
    public void testFailureWithSpringBoot2_0_6_RELEASE() {
        assertThat("<a foo=\"123\" bar=\"456\" />", isSimilarTo("<a foo=\"123\"/>"));
    }

In other words, a failed assertion.

The code path in the stacktrace snippet above only is triggered when the assertion fails.

When I put in a breakpoint, I saw the error message was to the effect that there were more attributes than expected. I figure I can play around with the configuration here to get the behavior I want (namely: "assert that all the attributes I listed are there, ignore stuff I didn't mention in my 'expected' case"). But, the issue seems to be that the code fails in a very messy way when an assertion fails, rather than just displaying the failure message it ought to display.

@krystiankaluzny

This comment has been minimized.

Member

krystiankaluzny commented Nov 28, 2018

Thanks for your verification. I have focused on checking artifacts versions and missed something obvious like test case. But even after adding suitable tests everything works fine, AssertionError is thrown but still no NoSuchMethodError.

I think that xmlunit is not an issue itself but it's your dependency configuration

I found this article describes similar problem
https://tedvinke.wordpress.com/2013/12/17/mixing-junit-hamcrest-and-mockito-explaining-nosuchmethoderror/
Base on it run mvn dependency:tree -Dscope=test and check if there is another hamcrest version on your classpath. If it is then try to exclude it.
You can also check if your IDE will find org.hamcrest.Matcher class that does not come from hamcrest:1.3 bud eg from mockito-all.

@pioto

This comment has been minimized.

pioto commented Nov 28, 2018

AHHH, indeed, there is some... lovely other, proprietary JAR I have in my classpath that includes org.hamcrest.Matcher...

I'll look at my options for fixing this then. Good catch.

@pioto pioto closed this Nov 28, 2018

@pioto

This comment has been minimized.

pioto commented Nov 28, 2018

For anyone else foolish enough to encounter this in the future, the fix was:

  • Liberal use of maven dependency exclusions, dependency management to clean up the classpath
  • Put anything that's trying to be "clever" and overwrite common classes in this way dead last in your <dependencies> list, so that the "real" ones win out in the classpath.

I'm now getting the expected java.lang.AssertionError, and now I just have to fix my tests :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment