I have method that throws an exception in special circumstances. I would like to write a test case that will check behaviour when exception is not thrown. I cannot find this in docs or examples. Please help.
E.g.:
when(validator.validate(any(ValidationData.class))).thenThrow(new ValidationException());
But I would like to test that exception is not thrown at all:
class Validator {
void validate(ValidationData dataToValidate) throws Exception {
}
}
e.g. I need something like:
when(doSomething()).thenNotThrowException
or
when(doSomething()).thenDoNothing
By default, Mockito's mock does nothing for void methods, so you don't need to write anything.
If you want to do this explicitly try this:
doNothing().when( validator ).validate( any() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With