What's the actual use of 'fail' in JUnit test case?

后端 未结 8 1145
长情又很酷
长情又很酷 2021-01-30 15:31

What\'s the actual use of \'fail\' in JUnit test case?

8条回答
  •  庸人自扰
    2021-01-30 15:51

    This is how I use the Fail method.

    There are three states that your test case can end up in

    1. Passed : The function under test executed successfully and returned data as expected
    2. Not Passed : The function under test executed successfully but the returned data was not as expected
    3. Failed : The function did not execute successfully and this was not

    intended (Unlike negative test cases that expect a exception to occur).

    If you are using eclipse there three states are indicated by a Green, Blue and red marker respectively.

    I use the fail operation for the the third scenario.

    e.g. : public Integer add(integer a, Integer b) { return new Integer(a.intValue() + b.intValue())}

    1. Passed Case : a = new Interger(1), b= new Integer(2) and the function returned 3
    2. Not Passed Case: a = new Interger(1), b= new Integer(2) and the function returned soem value other than 3
    3. Failed Case : a =null , b= null and the function throws a NullPointerException

提交回复
热议问题