Android 使用robolectric测试我的MapActivity

Android 使用robolectric测试我的MapActivity,android,google-maps,unit-testing,robolectric,Android,Google Maps,Unit Testing,Robolectric,我正在尝试使用robolectric进行测试,得到下面的错误。似乎其他人在create()之后添加意图时收到了此错误,但我根本没有操纵意图。也许我需要做一些特别的事情,因为它使用片段,但这只是一个猜测。任何帮助或指导都将不胜感激 堆栈跟踪: java.lang.NullPointerException at android.app.Activity.attach(Activity.java:4967) at org.fest.reflect.method.Invoker.invo

我正在尝试使用robolectric进行测试,得到下面的错误。似乎其他人在create()之后添加意图时收到了此错误,但我根本没有操纵意图。也许我需要做一些特别的事情,因为它使用片段,但这只是一个猜测。任何帮助或指导都将不胜感激

堆栈跟踪:

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() {

    }
}
我有同样的

以下是我目前的解决方法:

从ShadowMapActivity(从robolectric源获取)创建自定义阴影(例如ShadowMapActivityWorkaround)

进行以下更改:

public void __constructor__() {
    super.__constructor__();
}

将阴影添加到org.roblectric.Config.properties,例如:

shadows: com.mytests.shadows.ShadowMapActivityWorkaround

谢谢你的帮助。我试图实现你所说的,但我仍然得到同样的错误。下面是我的更新代码:Erik,不要在自定义阴影中扩展ShadowMapActivity,因为在本例中调用super.\uuu构造函数\uuuuu();在你的阴影下不会有任何效果。在ShadowMapActivity中,此方法仅为空。从这里获取代码重命名类名并添加更改。
@Implementation
public void onResume() {
    registerReceiver(connectivityBroadcastReceiver, new IntentFilter());
    field("mCalled").ofType(boolean.class).in(realActivity).set(true);
}
shadows: com.mytests.shadows.ShadowMapActivityWorkaround