我正在尝试使用robolectric测试并得到以下错误。似乎其他人在create()之后添加Intent时收到此错误但我根本没有操纵意图。也许我需要为此做一些特别的事情,因为它使用片段,但这只是猜测。任何帮助或指示将不胜感激。
堆栈跟踪:
java.lang.NullPointerException
at android.app.Activity.attach(Activity.java:4967)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController.attach(ActivityController.java:90)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:114)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at com.LocationsResultsMapTest.setUp(LocationsResultsMapTest.java:24)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
LocationsResultsMapTest.java
@RunWith(RobolectricTestRunner.class)
public class LocationsResultsMapTest {
private MockMapActivity activity;
private ActivityController<MockMapActivity> activityController;
@Before
public void setUp() throws Exception {
activityController = Robolectric.buildActivity(MockMapActivity.class);
activity = activityController.create().get();
}
@Test
public void test() {
}
}
MockMapActivity.java
public class MockMapActivity extends MapActivity {
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
}
答案 0 :(得分:1)
我有同样的issue。
以下是我目前的解决方法:
从ShadowMapActivity创建自定义阴影(例如ShadowMapActivityWorkaround)(从robolectric源获取)
进行以下更改:
public void __constructor__() {
super.__constructor__();
}
和
@Implementation
public void onResume() {
registerReceiver(connectivityBroadcastReceiver, new IntentFilter());
field("mCalled").ofType(boolean.class).in(realActivity).set(true);
}
将阴影添加到org.robolectric.Config.properties,例如:
shadows: com.mytests.shadows.ShadowMapActivityWorkaround