In my unit test using Mockito I want to verify that NullPointerException
was not thrown.
public void testNPENotThrown{
Calling calling= Mock(Calling.class);
testClass.setInner(calling);
testClass.setThrow(true);
testClass.testMethod();
verify(calling, never()).method();
}
My test set up the testClass
, setting the Calling
object and the property so that the method will throw a NullPointerException
.
I verify that the Calling.method() is never called.
public void testMethod(){
if(throw) {
throw new NullPointerException();
}
calling.method();
}
I want to have a failing test because it throws a NullPointerException
, and then I want to write some code to fix this.
What I have noticed is that the test always passes as the exception is never thrown up the the test method.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…