Given a NullPointerException as a unit test failure: is this good practice?

suppose you have a unit test that contains these lines

assertNotNull(someVal); assertNotEmpty(someVal); 

This, obviously, checks that someVal is not null and is filled with something.

The question is whether the first line is needed and does anything add to the test? If we do not have only the second line, and if it is null, this will throw a null pointer that still indicates a unit test failure (but not a failure).

What is the best practice in such simple cases?

+4
source share
4 answers

I'm not a big fan of assertNot() and tend to experience unexpected things that can happen to SUT, not to mention testing them in addition to the normal Assert in each individual test.

IMO, you should only check the nominal status of your facilities. If in some cases it is normal for someVal be null, create a special test method to verify that it is indeed null in this scenario. For your test, select a name that indicates the intent, for example someValShouldBeNullWhen...() . Do the same for empty or any other value.

You see that unexpected things happen to your SUTs by looking at your test exception messages, rather than trying to predict each of these unexpected things in advance and interrupt your tests with assertNots for them.

+2
source

You should not see NPE as a test failure. You should actually claim that it is not null and provide an error message,

assertNotNull(someVal, "Some descriptive message saying why value should not be null");

+4
source

It depends. Say you are testing the myFunc function. If myFunc returns null in non-exceptional cases, I consider it reasonable to verify that the result is not zero and the correct value, since it can make your test more clear.

If null is an exceptional case, then checking it is akin to catching a RuntimeException in your test. It just clutters your test and hides your intentions.

+4
source

In JUnit, there is a difference between error and error. An error is what is expected (this is explicitly checked with assertXXX() or fail() ), and an error is something that is not usually an exception. See What is the difference between error and error in JUnit? .

If you call

 assertNotNull(someVal); 

then you say that this value should not be zero, and you specifically check this. If you made a NullPointerException, then the person who interprets the error will not know if the code is checked correctly, or you just did not think about all the cases.

This is a matter of intent. It’s also nice to add an explanatory message, as others have pointed out.

+3
source

Source: https://habr.com/ru/post/1442045/


All Articles