Handle a possible exception in JUnit

@RunWith(AndroidJUnit4.class)
public class TestException {

    boolean isTestCompleted = false;
    private static final String TAG = TestException.class.getSimpleName();

    @Before
    public void setUp() throws Exception {
        Log.v(TAG, "Setup begins.");
    }

    @After
    public void tearDown() throws Exception {
        Log.v(TAG, "TearDown begins.");
        if (isTestCompleted) {
            Log.v(TAG, "Test is completed.");
        } else {
            Log.w(TAG, "Test is NOT completed!!!");
            //Something can be done here, capture a screenshot maybe.
        }
        Log.v(TAG, "TearDown finished.");
    }

    @Test
    public void testException() throws Exception {
        Log.v(TAG, "Test begins.");
        Log.v(TAG, String.valueOf(5 / 0));
        Log.v(TAG, "Test finishes.");
        isTestCompleted = true;
    }
}

Output:

03-18 20:26:58.736  4995  5011 I TestRunner: started: testException(com.XXX.TestException)
03-18 20:26:58.745  4995  5011 V TestException: Setup begins.
03-18 20:26:58.746  4995  5011 V TestException: Test begins.
03-18 20:26:58.746  4995  5011 V TestException: TearDown begins.
03-18 20:26:58.746  4995  5011 W TestException: Test is NOT completed!!!
03-18 20:26:58.746  4995  5011 V TestException: TearDown finished.
03-18 20:26:58.747  4995  5011 I TestRunner: failed: testException(com.XXX.TestException)
03-18 20:26:58.760  4995  5011 I TestRunner:    at com.XXX.TestException.testException(TestException.java:39)
03-18 20:26:58.779  4995  5011 I TestRunner: finished: testException(com.XXX.TestException)

While

@RunWith(AndroidJUnit4.class)
public class TestException {

    boolean isTestCompleted = false;
    private static final String TAG = TestException.class.getSimpleName();

    @Before
    public void setUp() throws Exception {
        Log.v(TAG, "Setup begins.");
    }

    @After
    public void tearDown() throws Exception {
        Log.v(TAG, "TearDown begins.");
        if (isTestCompleted) {
            Log.v(TAG, "Test is completed.");
        } else {
            Log.w(TAG, "Test is NOT completed!!!");
            //Something can be done here, capture a screenshot maybe.
        }
        Log.v(TAG, "TearDown finished.");
    }

    @Test
    public void testException() throws Exception {
        Log.v(TAG, "Test begins.");
        Log.v(TAG, String.valueOf(5 / 1));
        Log.v(TAG, "Test finishes.");
        isTestCompleted = true;
    }
}

Output:

03-18 20:33:28.567  5446  5462 I TestRunner: started: testException(com.XXX.TestException)
03-18 20:33:28.575  5446  5462 V TestException: Setup begins.
03-18 20:33:28.575  5446  5462 V TestException: Test begins.
03-18 20:33:28.575  5446  5462 V TestException: 5
03-18 20:33:28.575  5446  5462 V TestException: Test finishes.
03-18 20:33:28.575  5446  5462 V TestException: TearDown begins.
03-18 20:33:28.575  5446  5462 V TestException: Test is completed.
03-18 20:33:28.575  5446  5462 V TestException: TearDown finished.
03-18 20:33:28.576  5446  5462 I TestRunner: finished: testException(com.XXX.TestException)
This is useful when "@Test(timeout = MAIN_TIMEOUT)" is announced.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容