Search code examples
javajunit

Format of writing unit test cases for exceptions in junit


I am writing test cases for a Java class. It contains an exception case also where the exception is thrown when response from server is not as expected.

I am bit confused about the name of that test method.

The method is Book updateBook(Book book);

It throws some exception when right response is not received.

For that, I have written a unit test case. But I am not sure what format should I follow for correct naming of that test method.

Should I name it

  1. testThrowExceptionWhenResponseIsNull();

OR

  1. shouldThrowExceptionWhenBookManagementIsNull();

Solution

  • I would prefer

    updateBookThrowsExceptionWhenBookManagementIsNull
    

    It names which method is under test and the asserted result for which condition.

    But choose a name which is fitting for you und your colleagues.