解决android studio使用 Junit单元测试运行出现的问题 JUnit version 3.8 or later expected

写完测试单元测试用例后,点击运行出现以下错误,第一次用,有点蒙,网上查了半天,没找到结果。

!!! 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)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Process finished with exit code -3

后来想是不是没有把junit加到gradle中,果然,把

testCompile 'junit:junit:4.12'

加到gradle中,一运行,ok了,这里做个记录,给新手一个解决的办法。

dependencies {
    testCompile 'junit:junit:4.12'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile fileTree(include: ['*.jar'], dir: 'libs')
    // 其他依赖
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}

你可能感兴趣的:(技术分享,移动开发)