Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Robolectric模拟单击菜单项时出错_Android_Unit Testing_Robolectric - Fatal编程技术网

Android 使用Robolectric模拟单击菜单项时出错

Android 使用Robolectric模拟单击菜单项时出错,android,unit-testing,robolectric,Android,Unit Testing,Robolectric,我正在探索用于测试Android应用程序的Robolectric功能,我有一个简单的单元测试来验证activityNoteEditor类在单击菜单项时是否打开。代码是: @Before public void setup(){ // mProvider = new NotePadProvider(); mContentResolver = Robolectric.application.getContentResolver(); mProvider

我正在探索用于测试Android应用程序的Robolectric功能,我有一个简单的单元测试来验证activity
NoteEditor类
在单击菜单项时是否打开。代码是:

    @Before
    public void setup(){
    //
    mProvider = new NotePadProvider();
    mContentResolver = Robolectric.application.getContentResolver();
    mProvider.onCreate();
    ShadowContentResolver.registerProvider(NotePad.AUTHORITY, mProvider);
    activity = Robolectric.buildActivity(NotesList.class).create().start().resume().visible().get();
}

    @Test
    public void menuAddShouldStartTheEditorActivity() throws Exception {
    // create reference to menu item "menu_add"
    MenuItem item = new TestMenuItem(R.id.menu_add);
    // simulate click item
    activity.onOptionsItemSelected(item);
    ShadowActivity shadowActivity = shadowOf(activity);
    // get next started activity
    Intent startedIntent = shadowActivity.getNextStartedActivity();
    ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);

    assertThat(shadowIntent.getComponent().getClassName(), equalTo(NoteEditor.class.getName()));
}`
它只在第一段代码中起作用,但是当我执行代码行时:

ShadowIntent ShadowIntent=roblectric.shadowOf(startedIntent)

机器人分子给我

[Error]: java.lang.NullPointerException
    at com.example.roboelectric.notepadtest.SimpleTest.menuAddShouldStartTheEditorActivity(SimpleTest.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source) (...)

知道为什么吗?提前感谢

你不需要对意图视而不见。简单地做:

Intent startedIntent = shadowActivity.getNextStartedActivity();
assertThat(startedIntent.getComponent().getClassName(), equalTo(NoteEditor.class.getName()));

我绕过组件并使用意图验证正确的活动生命周期来解决它,所以我的测试用例变得

@Test
public void menuAddShouldStartTheEditorActivity() throws Exception {
// create reference to menu item "menu_add"
MenuItem item = new TestMenuItem(R.id.menu_add);
// simulate click item
activity.onOptionsItemSelected(item);
ShadowActivity shadowActivity = shadowOf(activity);
// get next started intent from shadow
Intent startedIntent = shadowActivity.getNextStartedActivity();
// define expected intent (Editor class)
Intent expectedIntent = new Intent(Intent.ACTION_INSERT, NoteColumns.CONTENT_URI);
// verify it 
assertThat(startedIntent, equalTo(expectedIntent));
}

我不知道为什么,但是getComponent()总是空的,因为AUT下一个活动(编辑器)中的onCreate()从未由Robolectric执行。

getNextStartedActivity()
将在此处返回空值。您想要精确测试什么?为什么为空?我想验证NoteEditor活动是否在
activity.onOptionsItemSelected(item)之后打开已执行…我已从中复制,我只更改单击菜单项而不是按钮(.performClick())…感谢您的回复您可以检查
startedIntent
是否为
null
吗?我不确定是否使用
System.out.println(“log:+startedIntent.getClass().getName())登录我在控制台上看到
日志:android.content.Intent
。。。如果我使用
System.out.println(“日志:+startedIntent.getComponent().getClassName())登录。。引发空指针错误..似乎startedIntent(对象)不是空的,而是空的派生组件?似乎
getComponent()
在此处返回
null
shadowIntent
也是
null
?您确定该行导致了
NullPointerException
?从注释中可以看出
getComponent()
返回@giovanni的
null
。嗨,Daniel谢谢您的回复,我按照您的建议简化了代码,但我始终接受NullPointer异常。我已经在AUT(测试中的应用程序)中注入了日志打印,以便按测试用例查看调用流。我注意到NotesList.java
startActivity(newintent(Intent.ACTION_INSERT,getIntent().getData())中的这段代码已执行,但从未打印登录NoteEditor.java类(注入
onCreate()
事件)。!Robolectric assert
(..)因为Robolectric是一个单元测试框架,活动实际上不会启动,但是我们可以检查活动是否触发了正确的意图(..)
,所以我认为这就是getComponent()始终返回null的原因。