Software Testing - Junit

Back to Course

Lesson Description

Lession - #1230 Junit Exceptions test

How to test exceptions in JUnit?

While utilizing JUnit , we can essentially utilize the normal attribute of the @Test annotation to proclaim that we anticipate that an exemption should be thrown anyplace in the annotated test method. In this model, we've announced that we're expecting our test code to bring about a NullPointerException.

How to write a test case for throwing another exception in JUnit?

To test the exception thrown by a method in JUnit , you should utilize @Test(expected=IllegalArgumentException. class>
comment. You can replace IllegalArgumentException. class with some other exception for example NullPointerException.

Let's see an example