NoSuchMethodError with Hamcrest 1.3 & JUnit 4.11

前端 未结 8 1331
独厮守ぢ
独厮守ぢ 2020-12-30 19:03

Another instance of the NoSuchMethodError for the JUnit & Hamcrest combination. Offending code:

assertThat(dirReader.document(0).getFields()         


        
相关标签:
8条回答
  • 2020-12-30 19:20

    What worked for me was to reorder dependencies. Instead of going mockito, junit, I had to put junit, mockito.

    Mockito 1.9.5 uses hamcrest 1.1 which is incompatible and causes problems.

    0 讨论(0)
  • 2020-12-30 19:24

    Using David's tip and How do I split a string on a delimiter in Bash? resulted in the following bash script:

    ( IFS=":"; for i in `mvn dependency:build-classpath | grep -v '\[INFO\]'`; do jar tf $i | awk "{print \"$i\\t\" \$1}"; done | grep Matcher )
    

    (online at http://www.kaspervandenberg.net/2013/scripts/findDependencyClass.sh)

    Which found that dependency JGlobus-Core-2.0.4 has its own versions of org.hamcrest.BaseMatcher, org.hamcrest.CoreMatchers, and org.hamcrest.Matcher.

    0 讨论(0)
  • 2020-12-30 19:26

    If you are using Eclipse: For me, in eclipse-> project properties->Java build Path moving mockito-all-1.9.5.jar to the bottom of the 'Order and Export' list did the trick. Just above that I have junit-4.11.jar and above that hamcrest-core-1.3.jar

    0 讨论(0)
  • 2020-12-30 19:37

    For a project with Gradle as a build tool:

    testCompile("junit:junit:4.11") {
         exclude group: 'org.hamcrest', module: 'hamcrest-core'
         exclude group: 'org.hamcrest', module: 'hamcrest-library' 
    }
    testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
    
    0 讨论(0)
  • 2020-12-30 19:38

    I solved this jar hell problem in my Gradle project with the code below:

        testCompile (group: 'junit', name: 'junit', version: '4+') {
            exclude group: 'org.hamcrest'
        }
        testCompile ('org.mockito:mockito-core:1+') {
            exclude group: 'org.hamcrest'
        }
        testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
    
    0 讨论(0)
  • 2020-12-30 19:39

    Perhaps one of those other JARs has older versions of Hamcrest's Matcher or BaseMatcher. Here's a list of JARs which include the latter, though I have no idea how comprehensive that site is. Is there a Maven plugin which will show you all the dependencies that include a class similar to the dependency tree?

    0 讨论(0)
提交回复
热议问题