Si supponga che voglio testare il codice come questo:Ottenere numero di chiamate ad un mock
class ClassToTest
// UsedClass1 contains a method UsedClass2 thisMethod() {}
UsedClass1 foo;
void aMethod()
{
int max = new Random().nextInt(100);
for(i = 0; i < max; i++)
{
foo.thisMethod().thatMethod();
}
}
}
Se ho un test come questo:
ClassToTest test;
UsedClass1 uc1;
UsedClass2 uc2;
@Test
public void thingToTest() {
test = new ClassToTest();
uc1 = mock(UsedClass1.class);
uc2 = mock(UsedClass2.class);
when(uc1.thisMethod()).thenReturn(uc2);
when(uc2.thatMethod()).thenReturn(true);
test.aMethod();
// I would like to do this
verifyEquals(callsTo(uc1.thisMethod()), callsTo(uc2.thatMethod()));
}
Come posso ottenere il numero di chiamate al uc1.thisMethod()
e uc2.thatMethod()
così posso verificare che entrambi sono stati chiamati lo stesso numero di volte?
L'unico modo in cui posso pensare (al momento) è posizionare un wrapper attorno a ciascun metodo, che incrementa wn contrastano ogni volta che viene chiamato. AFAIK il jvm non tiene traccia del numero di volte in cui un metodo/classe viene chiamato/istanziato. – munyul