Robolectric的getResources()抛出RuntimeException异常(Android)

5

我正在尝试使用Robolectric框架为我的Android项目创建单元测试。我从Eclipse Android项目向导创建的空白Android项目开始。

在另一个Java项目中,我使用以下代码尝试运行基本测试:

@RunWith(RobolectricTestRunner.class)
public class ApiTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void shouldHaveApplicationName() throws Exception {
        String appName = new RobolectricActivity().getResources().getString(R.string.app_name);
        assertEquals(appName, "TestDemo");
    }

    @Test
    public void testSomethingMeaningfulToMyApp() {
        fail("not implemented");
    }

}

我已经按照Robolectric快速入门指南中的说明进行了配置。
然而,当我试图运行这个测试类时,在访问getResources()函数的那一行代码处,我会遇到以下异常:
java.lang.RuntimeException: Did your shadow implementation of a method throw an exception? Refer to the bottom of this stack trace.
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:86)
at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
at android.content.ContextWrapper.getResources(ContextWrapper.java)
at com.xtremelabs.robolectric.shadows.ShadowContextWrapper.getResources(ShadowContextWrapper.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
at android.content.ContextWrapper.getResources(ContextWrapper.java)
at ApiTest.shouldHaveApplicationName(ApiTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:164)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
... 36 more
Caused by: java.lang.ExceptionInInitializerError
at android.os.Build$VERSION.<clinit>(Build.java:95)
at android.content.res.Resources.<clinit>(Resources.java:57)
at com.xtremelabs.robolectric.shadows.ShadowApplication.getResources(ShadowApplication.java:74)
... 41 more
Caused by: java.lang.NullPointerException
at android.os.SystemProperties.<clinit>(SystemProperties.java:35)
... 44 more

看起来阴影类没有正确设置,我在Google或Stackoverflow上找不到解决此问题的方法。

您有什么建议吗?非常感谢!

1个回答

0
你尝试过为测试类创建一个构造函数,调用你想要测试的Activity吗? 例如:
public class ApiTest {

    public ApiTest() {
        super(MainActivity.class);
    }

    //...the rest of your test class
}

我自己还在学习如何在Android中进行单元测试和模拟,所以如果这对你不起作用,我提前道歉。


哦,天啊;我刚意识到这个问题是来自2012年12月,而不是2013年 - 对于这个死灵法术感到抱歉! - NateCron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接