CodeGym /Kursus Jawa /3. modul /obyek moyoki

obyek moyoki

3. modul
tingkat , wulangan
kasedhiya

3.1 metode doReturn().

Saiki teka sihir ...

Ayo dadi ngomong sampeyan nggawe obyek mock palsu, nanging sampeyan kudu bisa piye wae. Nalika cara tartamtu diarani, ana sing penting ditindakake, utawa metode kasebut ngasilake asil tartamtu. Apa sing kudu ditindakake?

Perpustakaan Mockito ngidini sampeyan nambah prilaku sing dikarepake menyang obyek mock.

Yen sampeyan pengin obyek mock ngasilake asil tartamtu nalika cara tartamtu diarani, banjur "aturan" iki bisa ditambahake menyang obyek nggunakake kode:

Mockito.doReturn(result).when(an object).method name();

Sampeyan ndeleng, ing pungkasan panggilan metode, method name?sejatine ora ana telpon ing kene. Cara kasebut doReturn()ngasilake obyek proxy khusus kanthi bantuan sing ngawasi telpon saka metode obyek lan, kanthi mangkono, aturan kasebut ditulis.

maneh. Iku mung cara sing pinter kanggo nulis aturan kanggo nambah obyek mock . Perlu sawetara katrampilan kanggo nerjemahake kode kasebut kanthi bener nalika sampeyan ndeleng. Nerangake karo pengalaman.

Aku perlu conto konkrit. Ayo nggawe obyek kelas mock ArrayListlan takon cara size()kanggo ngasilake nomer 10. Kode lengkap bakal katon kaya iki:

@ExtendWith(MockitoExtension.class)
class DoReturnTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation () {
         //create a rule: return 10 when calling the size method
        Mockito.doReturn(10).when(mockList).size();

        //the method is called here and will return 10!!
        assertEquals(10, mockList.size());
    }
}

Ya, kode iki bakal bisa digunakake, tes ora bakal gagal.

3.2 nalika () metode

Ana cara liya kanggo nambah aturan prilaku menyang obyek mock - kanthi nelpon Mockito.when(). Katon kaya iki:

Mockito.when(an object.method name()).thenReturn(result);

Iki cara sing padha kanggo nulis aturan prilaku obyek mock minangka sadurunge. mbandhingake:

Mockito.doReturn(result).when(an object).method name();

Ing kene persis kedadeyan sing padha - pambangunan aturan anyar.

Bener, conto pisanan duwe rong minus:

  • telpon banget bingung.an object.method name()
  • ora bakal bisa yen cara methodname()bali void.

Inggih, ayo nulis conto favorit kita nggunakakeMockito.when()

@ExtendWith(MockitoExtension.class)
class WhenTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation() {
        //create a rule: return 10 when calling the size method
        Mockito.when(mockList.size() ).thenReturn(10);

        //the method is called here and will return 10!!
        assertEquals(10, mockList.size());
    }
}

3.3 metode doThrow().

Kita ngerti carane nggawe metode obyek mock ngasilake asil tartamtu. Kepiye carane bisa mbuwang pengecualian tartamtu? Kirim menyang doReturn()?

Kanggo nyegah cara bali, yaiku mbuwang pengecualian, sampeyan kudu nyetel aturan nggunakake doThrow().

Mockito.doThrow(exception.class).when(an object).method name();

Lan banjur pilihan kapindho:

Mockito.when(an object.method name()).thenThrow(exception.class);

A dicokot samesthine, tengen?

Inggih, sampeyan ndeleng, sampeyan wis wiwit ngerti. Ayo didandani nganggo conto:

@ExtendWith(MockitoExtension.class)
class DoThrowTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation() {
        Mockito.when(mockList.size() ).thenThrow(IllegalStateException.class);
        mockList.size(); //an exception will be thrown here
    }
}

Yen sampeyan kudu mbuwang obyek pangecualian tartamtu, gunakake konstruksi formulir kasebut:

Mockito.doThrow(new Exception()).when(an object).method name();

Mung pass doThrow()obyek pangecualian kanggo cara lan bakal di buwang sak telpon cara.

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION