这是我的测试用例:
public void testStartActivityWithoutExtraData() {
try {
getActivity();
Assert.fail("Should have thrown IllegalStateException");
} catch (IllegalStateException ex) {
assertTrue(true);
}
}
如果Activity
在没有Exception
的情况下启动,则extras
会引发IllegalStateException
。因此,在此测试中,我期望Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.IllegalStateException''. Check device logcat for details
Test running failed: Instrumentation run failed due to 'java.lang.IllegalStateException'
,但由于问题,测试总是失败:
Application
Logcat只是说Exception
引发了public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_item_activity);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
if (savedInstanceState == null) {
if (bundle != null) {
EditItemFragment editItemFragment = EditItemFragment.newInstance(bundle.getInt
("id"));
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.edit_item_activity_frame, editItemFragment).commit();
} else {
throw new IllegalStateException(TAG + " should be invoked with Extras (id for " +
"ticket)");
}
}
}
这是非常期待的。那么如何在测试中处理它呢?谢谢。
更新 这是我实际测试的代码:
{{1}}