Can we pass the junit test if the tested method throws the exception ? and Should we do that ?
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers

 Q1. Can we pass the junit test if the tested method throws the exception ? and Should we do that ?Junit
Ans. Yes, We can pass the test even if the tested method throws an exception.

One way is know as exception testing in which the tested method is expected to throw exception and hence the expectation is set accordingly.

@Test(expected = IndexOutOfBoundsException.class)
public void test(){}

Other way is to just ignore if the tested code throws an exception. In that case , we can enclose the tested method call in the try block and within exception block, we just return gracefully.

Exception testing is perfectly ok to be done as we would like test code in case an exception occurs and if the code is handling the exception properly. Ignoring the exception by putting the tested method call within try block isn't something normal and should be avoided.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Junit exception testing


Related Questions




Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: