java - Using both junit assertions and mockito verification -


i'm using junit in conjunction mockito. used mockito's verify method + junit assertion complete verification. not desirable? should use 1 or other not both?

there's nothing wrong using both.

mockito's verify used assert expect method(s) called expected value(s) on given mock(s).

junit's assertxyz used assert result desired result.

both valid verifications, , if both relevant, both should used.

for example, consider following (admittedly artificial) situation:

you have interface performs mathematical calculation:

public interface valueproducer {     public int getvalue(int val); } 

and class doubles result produced it:

public class doubler {     public static int doublethatresult (valueproducer producer, int val) {         return 2 * producer.getvalue(val);     } } 

testing require assert 2 things:

  1. that getvalue called
  2. that result doubled

so, e.g.:

public class doublertest {      @test     public void testdoublethatresult() throws exception {         int value = 7; // or other value         int returnmock = 13; // or other value          valueproducer producermock = mock(valueproducer.class);         when(producermock.getvalue(value)).thenreturn(returnmock);          int actual = doubler.doublethatresult(producermock, value);          verify(producermock);         assertequals(26, actual);     } }