maven - NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11 -
another instance of nosuchmethoderror
junit & hamcrest combination. offending code:
assertthat(dirreader.document(0).getfields(), hasitem( new featurematcher<indexablefield, string>(equalto("patisnummer"), "field key", "field key") { @override protected string featurevalueof(indexablefield actual) { return actual.name(); } } ));
commented lines 152–157 in indexertest.java (commit ac72ce)
causes nosuchmethoderror (see http://db.tt/qkkkte78 complete output):
java.lang.nosuchmethoderror: org.hamcrest.matcher.describemismatch(ljava/lang/object;lorg/hamcrest/description;)v @ org.hamcrest.featurematcher.matchessafely(featurematcher.java:43) @ org.hamcrest.typesafediagnosingmatcher.matches(typesafediagnosingmatcher.java:55) @ org.hamcrest.core.iscollectioncontaining.matchessafely(iscollectioncontaining.java:25) @ org.hamcrest.core.iscollectioncontaining.matchessafely(iscollectioncontaining.java:14) @ org.hamcrest.typesafediagnosingmatcher.matches(typesafediagnosingmatcher.java:55) @ org.junit.assert.assertthat(assert.java:770) @ org.junit.assert.assertthat(assert.java:736) @ indexer.indexertest.testindexcontainsfield(indexertest.java:152)
the setup:
- junit 4.11
- hamcrest 1.3
- using maven's surefire plugin (version 2.14), uses junitcoreprovider
- java 7 (openjdk)
- see pom (commit ac72ce)
background:
a nosuchmethoderror
caused (compiled) classes call non existing methods. specific case of describemismatch
, junit + hamcrest combination caused incompatibility between hamcrest classes included in junit , versions of classes in hamcrest library.
attempts solve nosuchmethoderror:
the pom contains explicit dependency on hamcrest-library 1.3, hamcrest-core 1.3, , junit 4.11, (in order) suggested garrett hall in answer getting "nosuchmethoderror: org.hamcrest.matcher.describemismatch" when running test in intellij 10.5
according junit documentation junit 4.11 maven dependency no longer include compiled hamcrest classes, instead has dependency on hamcrest-core 1.3;
nosuchmethoderror
should not occur.checking dependency tree
mvn dependency:tree
suggested dan in answer junit , hamcrest declaration shows explicit dependencies on hamcrest 1.3 , junit 4.11 , no other dependencies files (see http://db.tt/c2oftdjb complete output).in test
nosuchmethoderror
avoided using:assertthat( "zylab detector not available", d.getdetectors(), hasitem(matchers.<detector>instanceof(zylabmetadataxmldetector.class)));
in lines 120–123 of indexertest.java (commit ac72ce) instead of more obvious:
assertthat( "zylab detector not available", d.getdetectors(), hasitem(isa(zylabmetadatadetector.class));
i'm uncertain whether explicit type parameter
<detector>
, usinginstanceof
instead ofisa
, explicit reference hamcrest'smatchers
, or combination of avoidednosuchmethodexception
; after fiddling around , trying different things worked.using explicit type parameters did not solve/avoid error.
using class derived
basematcher
instead offeaturematcher
did not solve/avoid error.
ideas how fix nosuchmethoderror
?
this blog helped fix same problem me:
inside dependencies mockito , junit, author added excludes:
<dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <exclusions> <exclusion> <artifactid>hamcrest-core</artifactid> <groupid>org.hamcrest</groupid> </exclusion> </exclusions> </dependency>
Comments
Post a Comment