Java Robolectric测试从小部件启动的活动

Java Robolectric测试从小部件启动的活动,java,android,unit-testing,robolectric,Java,Android,Unit Testing,Robolectric,我有一个简单的小部件,它由一个图像和一个按钮组成,按钮应该启动一个活动。我正在尝试编写一个机器人分子测试,以测试在单击按钮时是否启动了活动 我有两个问题,首先,我在尝试单击按钮时遇到NPE: java.lang.NullPointerException: can't get a shadow for null at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:415) at org.robo

我有一个简单的小部件,它由一个图像和一个按钮组成,按钮应该启动一个活动。我正在尝试编写一个机器人分子测试,以测试在单击按钮时是否启动了活动

我有两个问题,首先,我在尝试单击按钮时遇到NPE:

java.lang.NullPointerException: can't get a shadow for null
   at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:415)
   at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1020)
   at org.robolectric.Robolectric.shadowOf(Robolectric.java:671)
   at org.robolectric.shadows.ShadowIntent.fillIn(ShadowIntent.java:454)
   at android.content.Intent.fillIn(Intent.java)
   at org.robolectric.shadows.ShadowPendingIntent.send(ShadowPendingIntent.java:48)
   at android.app.PendingIntent.send(PendingIntent.java)
   at org.robolectric.shadows.ShadowRemoteViews$2$1.onClick(ShadowRemoteViews.java:61)
   at android.view.View.performClick(View.java:4084)
另外,我也不知道如何通过点击按钮获得对活动的引用

测试代码:

@Test
public void buttonShouldLaunchActivity() throws Exception {
    int widgetId = shadowAppWidgetManager.createWidget(HelloWidgetProvider.class, R.layout.hellowidget_layout);
    View helloWidgetView = shadowAppWidgetManager.getViewFor(widgetId);
    Button quickButton = (Button) helloWidgetView.findViewById(R.id.quick_add_button);
    quickButton.performClick();

    // Not sure how to get a handle of the activity started from a widget, this is what I have for an activity launched from another activity.
    Intent intent = Robolectric.shadowOf(activity).peekNextStartedActivity();
    assertEquals(QuickAddActivity.class.getCanonicalName(), intent.getComponent().getClassName());
}

任何想法都值得一提,实际的小部件正在工作(活动已启动),但我只想对其进行测试。

peekNextStartedActivity()
实际上是ShadowApplication上的一个方法。ShadowContextWrapper上的方法(ShadowActivity隐式使用的方法)实际上只是一个在shadow应用程序上调用该方法的包装器

因此,您应该能够这样做,以获得您需要的:

Robolectric.getShadowApplication().peekNextStartedActivity()

你有什么进展吗?不幸的是没有,如果我有,我会更新这个。。。