Android 使用Robolectric模拟单击菜单项时出错
我正在探索用于测试Android应用程序的Robolectric功能,我有一个简单的单元测试来验证activityAndroid 使用Robolectric模拟单击菜单项时出错,android,unit-testing,robolectric,Android,Unit Testing,Robolectric,我正在探索用于测试Android应用程序的Robolectric功能,我有一个简单的单元测试来验证activityNoteEditor类在单击菜单项时是否打开。代码是: @Before public void setup(){ // mProvider = new NotePadProvider(); mContentResolver = Robolectric.application.getContentResolver(); mProvider
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.javastartActivity(newintent(Intent.ACTION_INSERT,getIntent().getData())中的这段代码代码>已执行,但从未打印登录NoteEditor.java类(注入onCreate()
事件)。!Robolectric assert(..)因为Robolectric是一个单元测试框架,活动实际上不会启动,但是我们可以检查活动是否触发了正确的意图(..)
,所以我认为这就是getComponent()始终返回null的原因。