Robolectric getResources()抛出RuntimeException(Android)

时间:2012-12-05 18:11:07

标签: android unit-testing robolectric

我正在尝试使用Robolectric框架为我的android项目创建单元测试。 我开始使用eclipse安卓项目向导创建的空项目。

在另一个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 Quick Start Guide中的说明配置了所有内容。

但是,当我尝试运行此测试类时,我在访问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 :(得分:0)

您是否尝试为调用您尝试测试的Activity的测试类创建构造函数? e.g:

public class ApiTest {

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

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

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