gpt4 book ai didi

java - 在单元测试期间在应用程序类中出错

转载 作者:行者123 更新时间:2023-11-30 01:16:12 24 4
gpt4 key购买 nike

当我试图通过 robolectric 执行我的应用程序类时,我遇到了奇怪的异常。 我被困了几个小时。 有人可以帮我吗?我的代码 fragment 如下:

 java.lang.RuntimeException: Stub!

at android.test.ApplicationTestCase.__constructor__(ApplicationTestCase.java:5)
at android.test.ApplicationTestCase.<init>(ApplicationTestCase.java)
at com.grapplemobile.tmcplus.TmcPlusApplicationTest.<init>(TmcPlusApplicationTest.java:26)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.createTest(RobolectricTestRunner.java:520)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.methodBlock(RobolectricTestRunner.java:530)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:247)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我的代码如下:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest extends ApplicationTestCase<MyApplication>{
private MyApplication application;

public MyApplicationTest ()
{
super(MyApplication.class);
}
@Before
public void setUp() throws Exception {
// super.setUp();
createApplication();
application = getApplication();

}

@Test
public void testonCreate() throws Exception {
application.onCreate();
}

最佳答案

不要将 JUnit 测试与仪器测试混合:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest{
}

如果您需要应用程序的实例,请使用 RuntimeEnvironment.application。如果你想测试应用程序本身,那么只需使用 new 创建它并调用它的方法,但它不应该是 Robolectric 测试

关于java - 在单元测试期间在应用程序类中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831563/

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