junit 运行时出现异常错误:存根控件

bwntbbo3  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(114)

我收到“java.lang.运行时异常:Stub!”。我有正确的eclipse顺序,也正确地设置了运行程序。这是堆栈跟踪。

java.lang.RuntimeException: java.lang.RuntimeException: Stub!
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Stub!
at android.net.Uri.parse(Uri.java:53)
at org.robolectric.shadows.ShadowMediaStore.reset(ShadowMediaStore.java:27)
at org.robolectric.Robolectric.reset(Robolectric.java:1351)
at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:47)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:226)
... 16 more

这是我的试卷

package com.example.sample.test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import com.example.sample.MainActivity;
import com.example.sample.R;

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
    private MainActivity activity;

    @Test
    public void shouldHaveHappySmiles() throws Exception {
        String hello = new MainActivity().getResources().getString(R.string.hello_world);
        assertThat(hello, equalTo("Hello world!"));
        activity = Robolectric.buildActivity(MainActivity.class)
                .create().get();
    }
}

我写这个测试用例只是为了和Roboelectric混在一起。

u0njafvf

u0njafvf1#

你不应该做:

new MainActivity().getResources().getString(R.string.hello_world);

这可能就是Stub!异常的来源,尽管它在堆栈跟踪中并不清楚。
相反,使用Robolectric创建您的Activity,然后获取hello字符串的值,然后Assert:

activity = Robolectric.buildActivity(MainActivity.class).create().start().resume().visible().get();
String hello = Robolectric.shadowOf(activity).getResources().getString(R.string.hello_world);
assertThat(hello, equalTo("Hello world!"));

您也可以使用ShadowApplication#getResources()。

相关问题