Is there a value to have more than one verification statement in the unit tests?

advertisements

Is there any reason to have more than one verify statement when testing a specific functionality - ie. verify that multiple/ or no dependent methods were called?

For Example:

public void doSomething(int size){
  if(size < 50){
    return;
   }
  someService.someMethod();
  anotherService.someMethod();
 }

To test this method

@Test
public void testDoSomethingWithSmallSize() throws Exception{
   testObj.doSomething(5);
   verify(someServiceMock, never()).someMethod();

  //IS THERE ANY VALUE TO VERFIYING MORE THAN ONE CALL?
  //LIKE THIS??
   verfiy(anotherServiceMock, never()).someMethod();
  }

Is there value to having the second verify statement or is it unnecessary because if the first statement wasn't called the second wasn't either?


You should verify the 2 statements because your code can change.

Unit test is some kind of documentation of your code.

  • Verifying both statements means that both statements MUST not be called.
  • Verifying only one means that only the 1st statement MUST not be called.

If someone changes the method to call "anotherService.someMethod()" inside the if statement, your test will still pass with 1 verify and will fail with 2 verify.