应使用 AssertNull 或 AssertNotNull

这是一个相当愚蠢的问题,但是我第一次使用单元测试,所以: 假设我有一个像 obj 这样的对象变量,如果 obj 为 Null,我希望我的单元测试为 Fail。 因此,对于断言,我应该说 AssertNull 还是 AssertNotNull。

304677 次浏览

assertNotNull asserts that the object is not null. If it is null the test fails, so you want that.

Use assertNotNull(obj). assert means must be.

The assertNotNull() method means "a passed parameter must not be null": if it is null then the test case fails.
The assertNull() method means "a passed parameter must be null": if it is not null then the test case fails.

String str1 = null;
String str2 = "hello";


// Success.
assertNotNull(str2);


// Fail.
assertNotNull(str1);


// Success.
assertNull(str1);


// Fail.
assertNull(str2);

I just want to add that if you want to write special text if It null than you make it like that

  Assert.assertNotNull("The object you enter return null", str1)