android.test.AndroidTestCase中的方法setUp没有被模拟

前端之家收集整理的这篇文章主要介绍了android.test.AndroidTestCase中的方法setUp没有被模拟前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 Android Studio的新单元测试功能.
我按照 http://tools.android.com/tech-docs/unit-testing-support上的说明进行操作.那里的描述明确提到了’Method … not mocked’错误,并建议将以下内容放入build.gradle:
android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

这可以在从命令行启动时运行测试

gradlew test --continue

但是当我使用rightclick从Android Studio运行测试类时 – >跑.这样,我又得到了同样的错误

java.lang.RuntimeException: Method setUp in android.test.AndroidTestCase not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.
    at android.test.AndroidTestCase.setUp(AndroidTestCase.java)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

关于如何解决这个问题的任何想法?

编辑:测试类的内容并不重要,因为测试的setUp失败,我尝试了最简单的类:

public class ContactFormToolTest extends AndroidTestCase {    

    public void testSOmething(){
        assertEquals(false,true);
    }            
}

也尝试重写setUp,没有任何区别.

解决方法

Android Studio中的新单元测试功能伪装整个Android SDK,因此您无需在Android设备上安装应用程序即可运行快速的纯Java测试(这类似于Robolectric).一般的想法是你模拟Android SDK调用的所有响应.

AndroidTestCase用于使用真正的Android SDK运行测试.

因此,您的问题是您正在尝试运行依赖于Android SDK的AndroidTestCase,但您的测试运行器正在启动单元测试环境,该环境使用假的Android SDK而不是真实的SDK.

你需要选择一种方法.如果你想要一个纯单元测试,那么你可能应该使用JUnit 4测试类而不是AndroidTestCase.更多说明:
https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#build

原文链接:https://www.f2er.com/android/317517.html

猜你在找的Android相关文章