如果我尝试在Android Studio中运行单元测试,则总是会收到此错误:
!!! JUnit version 3.8 or later expected:
java.lang.ClassNotFoundException: junit.framework.ComparisonFailure
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:221)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:207)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:61)
我的gradle依赖项看起来像这样:
def google_services = "15.0.1"
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// for tests
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.powermock:powermock-api-mockito2:1.7.4'
androidTestImplementation 'org.powermock:powermock-module-junit4:1.7.4'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:gridlayout-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation "com.google.android.gms:play-services-maps:${google_services}"
implementation "com.google.android.gms:play-services-location:${google_services}"
implementation "com.google.android.gms:play-services-gcm:${google_services}"
implementation "com.google.android.gms:play-services-analytics:16.0.1"
implementation "com.google.firebase:firebase-messaging:17.1.0"
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.android.exoplayer:exoplayer:2.6.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.androidmapsextensions:android-maps-extensions:2.2.0'
implementation 'com.squareup:otto:1.3.8'
implementation 'com.squareup.retrofit:retrofit:1.9.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
implementation 'com.github.rtoshiro.securesharedpreferences:securesharedpreferences:1.2.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true
}
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.zcw:togglebutton-library:1.0.0'
implementation 'com.android.support:exifinterface:27.1.1'
}
我的测试如下:
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
@Test
public void testTestIt() {
// some test code
}
}
我尝试了以下所有“解决方案”,但没有一个起作用:
有人可以指出我正确的方向吗?
更多信息: