1

这是我的测试用例:

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

如果ActivityException没有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'

Logcat 只是说Application抛出了一个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)");
            }
        }
    }
4

0 回答 0