title: NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11
body: <p>Another instance of the <code>NoSuchMethodError</code> for the JUnit &amp; Hamcrest combination.
  Offending code:</p>
  
  <pre><code>assertThat(dirReader.document(0).getFields(), hasItem(
      new FeatureMatcher&lt;IndexableField, String&gt;(equalTo("Patisnummer"), "Field key", "Field key") {
          @Override
          protected String featureValueOf(IndexableField actual) {
              return actual.name();
          } } ));
  </code></pre>
  
  <p>Commented lines 152–157 in <a href="https://github.com/kaspervandenberg/aida/blob/aida-9_linkXmlMetaDataAndAddFields/Search/Indexer/src/test/java/indexer/IndexerTest.java" rel="noreferrer">IndexerTest.java</a> (commit <a href="https://github.com/kaspervandenberg/aida/commit/ac72ce1b73db8e48fa721bdce7d93a8ee36871bd" rel="noreferrer">ac72ce</a>)</p>
  
  <p>Causes a NoSuchMethodError (see <a href="http://db.tt/qkkkTE78" rel="noreferrer">http://db.tt/qkkkTE78</a> for complete output):</p>
  
  <pre><code>java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
  at org.hamcrest.FeatureMatcher.matchesSafely(FeatureMatcher.java:43)
  at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
  at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:25)
  at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
  at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
  at org.junit.Assert.assertThat(Assert.java:770)
  at org.junit.Assert.assertThat(Assert.java:736)
  at indexer.IndexerTest.testIndexContainsField(IndexerTest.java:152)
  </code></pre>
  
  <h2>The setup:</h2>
  
  <ul>
  <li>JUnit 4.11</li>
  <li>Hamcrest 1.3</li>
  <li>Using Maven's surefire plugin (version 2.14), which uses its JUnitCoreProvider</li>
  <li>Java 7 (OpenJDK)</li>
  <li>See <a href="https://github.com/kaspervandenberg/aida/blob/aida-9_linkXmlMetaDataAndAddFields/Search/Indexer/pom.xml" rel="noreferrer">pom</a> (commit <a href="https://github.com/kaspervandenberg/aida/commit/ac72ce1b73db8e48fa721bdce7d93a8ee36871bd" rel="noreferrer">ac72ce</a>)</li>
  </ul>
  
  <h2>Background:</h2>
  
  <p>A <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/NoSuchMethodError.html" rel="noreferrer"><code>NoSuchMethodError</code></a> is caused by (compiled) classes that call non existing methods.  The specific case of <code>describeMismatch</code> and the JUnit + Hamcrest combination is often caused by an incompatibility between Hamcrest classes included in JUnit and versions of those classes in the Hamcrest library.</p>
  
  <h2>Attempts to solve the NoSuchMethodError:</h2>
  
  <ul>
  <li><p>The pom contains an explicit dependency on Hamcrest-library 1.3, Hamcrest-core 1.3, and JUnit 4.11, (in that order) as suggested by <a href="https://stackoverflow.com/users/554988/garrett-hall">Garrett Hall</a> in <a href="https://stackoverflow.com/a/7878281/814206">answer</a> to <a href="https://stackoverflow.com/questions/7869711/getting-nosuchmethoderror-org-hamcrest-matcher-describemismatch-when-running">Getting &quot;NoSuchMethodError: org.hamcrest.Matcher.describeMismatch&quot; when running test in IntelliJ 10.5</a></p></li>
  <li><p>According to the JUnit documentation the JUnit 4.11 Maven dependency does no longer include compiled Hamcrest classes, instead it has a dependency on Hamcrest-core 1.3; so the <code>NoSuchMethodError</code> should not occur.</p></li>
  <li><p>Checking the dependency tree with <code>mvn dependency:tree</code> as suggested by <a href="https://stackoverflow.com/users/545137/dan">Dan</a> in <a href="https://stackoverflow.com/a/15709116/814206">answer</a> to <a href="https://stackoverflow.com/questions/15660807/junit-and-hamcrest-declaration">junit and hamcrest declaration</a> shows the explicit dependencies on Hamcrest 1.3 and JUnit 4.11 and no other dependencies to those files (see <a href="http://db.tt/C2OfTDJB" rel="noreferrer">http://db.tt/C2OfTDJB</a> for the complete output).</p></li>
  <li><p>In another test the <code>NoSuchMethodError</code> was avoided by using:</p>
  
  <pre><code>assertThat(
      "Zylab detector not available",
      d.getDetectors(),
      hasItem(Matchers.&lt;Detector&gt;instanceOf(ZylabMetadataXmlDetector.class)));
  </code></pre>
  
  <p>In lines 120–123 of <a href="https://github.com/kaspervandenberg/aida/blob/aida-9_linkXmlMetaDataAndAddFields/Search/Indexer/src/test/java/indexer/IndexerTest.java" rel="noreferrer">IndexerTest.java</a> (commit <a href="https://github.com/kaspervandenberg/aida/commit/ac72ce1b73db8e48fa721bdce7d93a8ee36871bd" rel="noreferrer">ac72ce</a>)
  instead of the more obvious:</p>
  
  <pre><code>assertThat(
      "Zylab detector not available",
      d.getDetectors(),
      hasItem(isA(ZylabMetadataDetector.class));
  </code></pre>
  
  <p>I'm uncertain whether the explicit type parameter <code>&lt;Detector&gt;</code>, using <code>instanceOf</code> instead of <code>isA</code>, the explicit reference to Hamcrest's <code>Matchers</code>, or a combination of those avoided the <code>NoSuchMethodException</code>; after fiddling around and trying different things it worked.</p></li>
  <li><p>Using explicit type parameters did not solve/avoid the error.</p></li>
  <li><p>Using a class derived from <code>BaseMatcher</code> instead of <code>FeatureMatcher</code> did not solve/avoid the error.</p></li>
  </ul>
  
  <h2>Ideas how fix the <code>NoSuchMethodError</code>?</h2>