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

栏目: IOS · Android · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/28761267/method-setup-in-android-test-androidtestcase-not-mocked

我正在尝试使用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://stackoverflow.com/questions/28761267/method-setup-in-android-test-androidtestcase-not-mocked


以上所述就是小编给大家介绍的《android.test.AndroidTestCase中的方法setUp没有被模拟》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP Cookbook

PHP Cookbook

Adam Trachtenberg、David Sklar / O'Reilly Media / 2006-08-01 / USD 44.99

When it comes to creating dynamic web sites, the open source PHP language is red-hot property: used on more than 20 million web sites today, PHP is now more popular than Microsoft's ASP.NET technology......一起来看看 《PHP Cookbook》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具