2014-02-15 292 views
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); 
    } 
} 

回答

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); 
} 

add t他影子org.robolectric.Config.properties例如:

shadows: com.mytests.shadows.ShadowMapActivityWorkaround 
+0

感谢您的帮助。我试图实现你在说什么,但我仍然遇到同样的错误。这里是我更新的代码:https://gist.github.com/anonymous/9120783 –

+1

Erik,不要在你的自定义阴影中扩展ShadowMapActivity,因为在这种情况下,调用super .__构造函数__();在你的影子里不会有任何影响。在ShadowMapActivity中,这个方法只是空白。从这里获取代码https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowMapActivity.java重命名类名并添加更改。 –