如何期待 Robotium 中的异常?

2023-11-29

这是我的测试用例:

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

The 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'

Logcat 只是说Application扔了一个Exception这是非常令人期待的。那么在测试中该如何处理呢?谢谢。

Update这是我实际测试的代码:

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)");
            }
        }
    }

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何期待 Robotium 中的异常? 的相关文章

随机推荐