美文网首页Android
Handle a possible exception in J

Handle a possible exception in J

作者: JaedenKil | 来源:发表于2018-03-19 11:36 被阅读2次
    @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.

    相关文章

      网友评论

        本文标题:Handle a possible exception in J

        本文链接:https://www.haomeiwen.com/subject/dqwuqftx.html