0
我正在嘗試使用robolectric進行測試並獲取下面的錯誤。看起來像其他人在create()後添加一個Intent時收到了這個錯誤,但我根本沒有在操作這個intent。也許我需要爲此做一些特殊的事情,因爲它使用了片段,但這只是一個猜測。任何幫助或方向將不勝感激。使用robolectric測試我的MapActivity
堆棧跟蹤:
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);
}
}
感謝您的幫助。我試圖實現你在說什麼,但我仍然遇到同樣的錯誤。這裏是我更新的代碼:https://gist.github.com/anonymous/9120783 –
Erik,不要在你的自定義陰影中擴展ShadowMapActivity,因爲在這種情況下,調用super .__構造函數__();在你的影子裏不會有任何影響。在ShadowMapActivity中,這個方法只是空白。從這裏獲取代碼https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowMapActivity.java重命名類名並添加更改。 –