Hamcrest 中的 NoSuchMethodError Android 单元测试
NoSuchMethodError in Hamcrest with Android unit test
我试图在我的 Android JUnit4 单元测试中使用 ExpectedException @Rule,但是测试失败 java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch
。
其他问题表明我的其他库中有一个旧版本的 Hamcrest,但情况似乎并非如此。我已将 Hamcrest 从所有其他库中排除,并明确包括 Hamcrest 1.3。
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile ('junit:junit:4.12') {
exclude module: 'hamcrest-core'
}
testCompile ('org.mockito:mockito-core:1.10.19') {
exclude module: 'hamcrest-core'
}
testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile ('org.powermock:powermock-module-junit4:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
这是依赖关系图。
+--- org.hamcrest:hamcrest-core:1.3
+--- junit:junit:4.12
+--- org.mockito:mockito-core:1.10.19
| \--- org.objenesis:objenesis:2.1
+--- org.powermock:powermock-api-mockito:1.6.2
| +--- org.mockito:mockito-all:1.10.19
| \--- org.powermock:powermock-api-support:1.6.2
| +--- org.powermock:powermock-core:1.6.2
| | +--- org.powermock:powermock-reflect:1.6.2
| | \--- org.javassist:javassist:3.19.0-GA
| \--- org.powermock:powermock-reflect:1.6.2
\--- org.powermock:powermock-module-junit4:1.6.2
+--- junit:junit:4.12
\--- org.powermock:powermock-module-junit4-common:1.6.2
+--- junit:junit:4.4 -> 4.12
+--- org.powermock:powermock-core:1.6.2 (*)
\--- org.powermock:powermock-reflect:1.6.2
如果我不将 Hamcrest 作为显式依赖项包含在内,那么我会得到一个 ClassNotFoundException: org.hamcrest.TypeSafeMatcher
,因此不会包含任何其他内容。
我正在使用 Android Studio 1.3.2 和 Gradle 插件 1.2.3。
清理 Android Studio 中的缓存(文件->无效 Caches/Restart)解决了我的问题。不需要从 JUnit 和 Mockito 中排除 Hamcrest。
我试图在我的 Android JUnit4 单元测试中使用 ExpectedException @Rule,但是测试失败 java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch
。
其他问题表明我的其他库中有一个旧版本的 Hamcrest,但情况似乎并非如此。我已将 Hamcrest 从所有其他库中排除,并明确包括 Hamcrest 1.3。
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile ('junit:junit:4.12') {
exclude module: 'hamcrest-core'
}
testCompile ('org.mockito:mockito-core:1.10.19') {
exclude module: 'hamcrest-core'
}
testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile ('org.powermock:powermock-module-junit4:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
这是依赖关系图。
+--- org.hamcrest:hamcrest-core:1.3
+--- junit:junit:4.12
+--- org.mockito:mockito-core:1.10.19
| \--- org.objenesis:objenesis:2.1
+--- org.powermock:powermock-api-mockito:1.6.2
| +--- org.mockito:mockito-all:1.10.19
| \--- org.powermock:powermock-api-support:1.6.2
| +--- org.powermock:powermock-core:1.6.2
| | +--- org.powermock:powermock-reflect:1.6.2
| | \--- org.javassist:javassist:3.19.0-GA
| \--- org.powermock:powermock-reflect:1.6.2
\--- org.powermock:powermock-module-junit4:1.6.2
+--- junit:junit:4.12
\--- org.powermock:powermock-module-junit4-common:1.6.2
+--- junit:junit:4.4 -> 4.12
+--- org.powermock:powermock-core:1.6.2 (*)
\--- org.powermock:powermock-reflect:1.6.2
如果我不将 Hamcrest 作为显式依赖项包含在内,那么我会得到一个 ClassNotFoundException: org.hamcrest.TypeSafeMatcher
,因此不会包含任何其他内容。
我正在使用 Android Studio 1.3.2 和 Gradle 插件 1.2.3。
清理 Android Studio 中的缓存(文件->无效 Caches/Restart)解决了我的问题。不需要从 JUnit 和 Mockito 中排除 Hamcrest。