I am trying to use the Robolectric platform to create unit tests for my Android projects. I started with an empty Android project created by the eclipse android project wizard.
In another Java project, I use this code to try a basic test:
@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"); } }
I configured everything as described in the Robolectric Quick Start Guide .
However, when I try to run this test class, I get the following exception in the line where I access the getResources () function:
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
It seems that the shadow classes are not configured properly and I have not found a solution to this problem in google or stackoverflow.
Do you have any suggestions for me? Thank you very much!
source share