gpt4 book ai didi

android - android.test.AndroidTestCase 中的方法 setUp 未被模拟

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:00 26 4
gpt4 key购买 nike

我正在尝试接受 Android Studio 的新单元测试功能。我已按照 http://tools.android.com/tech-docs/unit-testing-support 上的说明进行操作.那里的描述明确提到了“方法 ... 未模拟”错误,并建议将以下内容放入 build.gradle:

android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}

只要从命令行启动时运行测试,这就有效

gradlew test --continue

但当我通过右键单击 -> 运行从 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)

关于如何解决这个问题有什么想法吗?

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

public class ContactFormToolTest extends AndroidTestCase {    

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

还尝试覆盖 setUp,没有区别。

最佳答案

发件人:https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito). If that proves problematic, you can add the snippet below to your build.gradle to change this behavior:

android {

// ...

testOptions {
unitTests.returnDefaultValues = true
}
}

关于android - android.test.AndroidTestCase 中的方法 setUp 未被模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com