It means your unit tests are incomplete: you have to test both valid and invalid situations. As you indicate yourself: you want to know when something works but you also want to know when it fails.
Consider the following two examples:
@Test(expected = PersonNotFoundException.class)
public void GetPerson_WithNonExistingId_ShouldThrowPersonNotFoundException(){
Person person = new PersonRepository().getPersonById(-1);
}
@Test
public void GetPerson_WithExistingId_ShouldReturnPerson(){
Person person = new PersonRepository().getPersonById(5);
Assert.AssertNotNull(person);
}
Now you have two tests that will verify the method's behaviour both when it is supposed to work and when it shouldn't. If you now suddenly change the implementation of PersonRepository.getPersonById(int)
to always throw PersonNotFoundException
, your first test will still succeed but the other one won't.
It is advised to validate the spectrum of distinct input paths. This often includes
- Passing
null
values
- Passing out-of-bound values
- Passing negative values
- Passing non-existing values
etc
I realized too late that Moq is C#, not Java. This idea is language-agnostic though and the code speaks for itself.