How to expect an exception in Robotium?

0
0

Here is my test case:

public void testStartActivityWithoutExtraData() {
try {
getActivity
();
Assert.fail("Should have thrown IllegalStateException");
} catch (IllegalStateException ex) {
assertTrue
(true);
}
}

The Activity under a test throws an Exception if it is started without extras. So in this test I expect for an IllegalStateException, but test always fails because of the issue:

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'

Logcat is just saying the Application thrown an Exception which is very expected. So how to handle it in a test? Thank you.

Update
Here is the code I’m actually testing:

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)");
}
}
}
  • You must to post comments
Showing 0 results
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website