5.1 Metode verifikasi ().
Saliyane nggawe obyek virtual, tugas menarik liyane asring muncul - kanggo mesthekake yen kelas sing diuji nelpon cara sing bener saka obyek sing bener. Menapa malih, piyambakipun nimbali kaping ingkang dipunbetahaken, kanthi paramèter ingkang leres, lan sanèsipun.
Kanggo iki, Mockito uga duwe sawetara sihir - kulawarga metode Mockito.verify(…)
. Aturan umum sing nemtokake cara mriksa telpon yaiku:
Mockito.verify(an object).method name(parameter);
Tuladha:
@ExtendWith(MockitoExtension.class)
class VerifyTest {
@Mock
List<String> mockList;
@Test
public void whenMockAnnotation() {
//method call
String name = mockList.get(10);
// check if the method was called
Mockito.verify(mockList).get(10);
}
}
Sajrone panggilan metode, verify()
kita nyetel aturan yen mockitoList
metode kanthi parameter 10 kudu diarani obyek get()
.
5.2 Cara verifikasi () karo mriksa nomer telpon
Kadhangkala ana skenario verifikasi sing luwih rumit. Contone, sampeyan kudu mriksa ora mung kasunyatan sing cara iki disebut, nanging, contone, iku disebut 3 kaping. Utawa diarani saka sampeyan ing siklus lan mulane kudu disebut N kaping.
Kita ora bakal takon apa iki bisa ditindakake, kita bakal langsung takon: carane nulis aturan kasebut? Lan maneh, Mockito ora nguciwani kita. Aturan kasebut bisa ditemtokake minangka:
Mockito.verify(an object,quantity).method name(parameter);
Penting! Kuantitas dudu jinis int
, nanging obyek khusus sing bisa nemtokake macem-macem pola. Apa sampeyan ngelingi macem-macem versi metode kasebut any()
? Iku padha ing kene - ana cara khusus sing sampeyan bisa nyetel macem-macem skenario:
Metode | Katrangan | |
---|---|---|
1 | ora tau() | Cara kasebut ora kudu diarani |
2 | kaping (n) | n kaping |
3 | paling (n) | n utawa kaping liyane |
4 | atLeastOnce() | 1 utawa kaping liyane |
5 | paling(n) | n utawa kurang kaping |
6 | mung() | Mesthine mung siji telpon lan mung kanggo metode iki |
Tuladha:
String name1 = mockList.get(1); //method call
String name2 = mockList.get(2); //method call
String name3 = mockList.get(3); //method call
//check that the get() method was called 3 times
Mockito.verify(mockList, times(3)).get(anyInt());
Sampeyan uga bisa mbutuhake, kajaba saka cara sing ditemtokake, ora ana referensi liyane kanggo obyek kasebut . Ana aturan kanggo iki:
Mockito.verifyNoMoreInteractions(an object);
5.3 Urutan telpon cara
Aturan sadurunge ora ngatur urutan cara nelpon kanthi cara apa wae. Aturan kasebut mung kudu ditindakake lan wis rampung. Nanging ana kahanan nalika urutan cara telpon penting, lan Mockito uga duwe solusi kanggo iki.
Urutan panggilan cara sing ketat bisa ditemtokake nggunakake obyek khusus InOrder
. Pisanan sampeyan kudu nggawe:
InOrder inOrder = Mockito.inOrder(an object);
Banjur tambahake aturan kasebut kanthi cara nelpon verify()
.
Tuladha:
List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.add("a parameter");
mockedList.clear();
InOrder inOrder = Mockito.inOrder(mockedList);
inOrder.verify(mockedList).size();
inOrder.verify(mockedList).add("a parameter");
inOrder.verify(mockedList).clear();
5.4 Priksa pangecualian ing Mockito
Kasunyatan yen pangecualian wis dibuwang dicenthang rada beda. Kanggo nindakake iki, gunakake metode kasebut assertThrows()
. Format umum kanggo mriksa kasebut yaiku:
Assertions.assertThrows(exception.class, () -> an object.methodname());
Ora ana sing rumit.
Tuladha:
@ExtendWith(MockitoExtension.class)
class ThenThrowTest {
@Mock
List mockList;
@Test
public void whenMockAnnotation() {
//set the behavior of the method (needed for demonstration purposes only)
Mockito.when(mockList.size()).thenThrow(IllegalStateException.class);
//check if an IllegalStateException will be thrown when calling the size method
assertThrows(IllegalStateException.class, () -> mockList.size());
}
}
GO TO FULL VERSION