JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案_第1頁
JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案_第2頁
JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案_第3頁
JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案_第4頁
JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

JAVA中MockitoMock測試的實踐與應(yīng)用試題及答案姓名:____________________

一、單項選擇題(每題2分,共10題)

1.以下關(guān)于Mockito的說法錯誤的是:

A.Mockito是一個開源的Java框架,用于模擬對象和測試代碼

B.Mockito主要用于單元測試,可以提高代碼的可測試性

C.Mockito需要通過依賴注入的方式將模擬對象注入到測試對象中

D.Mockito不支持模擬Java的內(nèi)置類,如String、List等

2.在使用Mockito進行模擬時,以下哪個方法可以創(chuàng)建一個模擬對象?

A.Mockito.mock(Class)

B.Mockito.spy(Class)

C.Mockito.mock(Class,Class)

D.Mockito.spy(Object)

3.以下關(guān)于Mockito的哪個方法可以驗證對象是否被調(diào)用過?

A.Mockito.verify(Object,times(int))

B.Mockito.when(Object).thenReturn(value)

C.Mockito.doReturn(value).when(Object)

D.Mockito.doThrow(Exception.class).when(Object)

4.在使用Mockito時,以下哪個方法可以設(shè)置模擬對象的方法返回值?

A.Mockito.when(Object).thenReturn(value)

B.Mockito.doReturn(value).when(Object)

C.Mockito.verify(Object,times(int))

D.Mockito.doThrow(Exception.class).when(Object)

5.以下關(guān)于Mockito的哪個方法可以設(shè)置模擬對象的方法拋出異常?

A.Mockito.when(Object).thenThrow(Exception.class)

B.Mockito.doThrow(Exception.class).when(Object)

C.Mockito.verify(Object,times(int))

D.Mockito.doReturn(value).when(Object)

6.在使用Mockito進行模擬時,以下哪個方法可以驗證模擬對象的方法調(diào)用次數(shù)?

A.Mockito.verify(Object,times(int))

B.Mockito.when(Object).thenReturn(value)

C.Mockito.doReturn(value).when(Object)

D.Mockito.doThrow(Exception.class).when(Object)

7.以下關(guān)于Mockito的哪個方法可以設(shè)置模擬對象的方法執(zhí)行特定操作?

A.Mockito.doCallRealMethod().when(Object)

B.Mockito.when(Object).thenThrow(Exception.class)

C.Mockito.doReturn(value).when(Object)

D.Mockito.verify(Object,times(int))

8.在使用Mockito進行模擬時,以下哪個方法可以設(shè)置模擬對象的方法執(zhí)行返回值和拋出異常?

A.Mockito.doReturn(value).when(Object).thenThrow(Exception.class)

B.Mockito.when(Object).thenThrow(Exception.class).thenReturn(value)

C.Mockito.doThrow(Exception.class).when(Object)

D.Mockito.verify(Object,times(int))

9.以下關(guān)于Mockito的哪個方法可以設(shè)置模擬對象的方法執(zhí)行返回值和調(diào)用其他模擬方法?

A.Mockito.when(Object).thenReturn(value).thenCallRealMethod()

B.Mockito.doReturn(value).when(Object)

C.Mockito.doThrow(Exception.class).when(Object)

D.Mockito.verify(Object,times(int))

10.在使用Mockito進行模擬時,以下哪個方法可以設(shè)置模擬對象的方法執(zhí)行返回值、拋出異常和調(diào)用其他模擬方法?

A.Mockito.when(Object).thenThrow(Exception.class).thenCallRealMethod().thenReturn(value)

B.Mockito.doReturn(value).when(Object).thenThrow(Exception.class)

C.Mockito.doThrow(Exception.class).when(Object)

D.Mockito.verify(Object,times(int))

二、多項選擇題(每題3分,共10題)

1.Mockito的優(yōu)點包括:

A.提高代碼的可測試性

B.隔離測試邏輯

C.提高代碼的復(fù)用性

D.優(yōu)化開發(fā)效率

E.降低代碼的復(fù)雜性

2.Mockito支持的模擬對象類型包括:

A.Java的內(nèi)置類

B.自定義類

C.接口

D.枚舉

E.數(shù)組

3.Mockito的常見使用場景有:

A.單元測試

B.集成測試

C.功能測試

D.性能測試

E.安全測試

4.Mockito提供的驗證方法有:

A.Mockito.verify(Object,times(int))

B.Mockito.verify(Object,atLeastOnce())

C.Mockito.verify(Object,atLeast(int))

D.Mockito.verify(Object,atMost(int))

E.Mockito.verify(Object,never())

5.Mockito提供的設(shè)置返回值的方法有:

A.Mockito.when(Object).thenReturn(value)

B.Mockito.doReturn(value).when(Object)

C.Mockito.when(Object).thenThrow(Exception.class)

D.Mockito.doThrow(Exception.class).when(Object)

E.Mockito.when(Object).thenCallRealMethod()

6.Mockito提供的設(shè)置異常拋出的方法有:

A.Mockito.when(Object).thenThrow(Exception.class)

B.Mockito.doThrow(Exception.class).when(Object)

C.Mockito.when(Object).thenReturn(value)

D.Mockito.doReturn(value).when(Object)

E.Mockito.when(Object).thenCallRealMethod()

7.Mockito提供的驗證調(diào)用順序的方法有:

A.Mockito.verifyInOrder(Object)

B.Mockito.verifyOrder(Object)

C.Mockito.verifySequence(Object)

D.Mockito.verifySequentially(Object)

E.Mockito.verifyInSequence(Object)

8.Mockito提供的設(shè)置調(diào)用次數(shù)的方法有:

A.Mockito.verify(Object,times(int))

B.Mockito.verify(Object,atLeastOnce())

C.Mockito.verify(Object,atLeast(int))

D.Mockito.verify(Object,atMost(int))

E.Mockito.verify(Object,never())

9.Mockito提供的設(shè)置方法執(zhí)行結(jié)果的方法有:

A.Mockito.when(Object).thenReturn(value)

B.Mockito.doReturn(value).when(Object)

C.Mockito.when(Object).thenThrow(Exception.class)

D.Mockito.doThrow(Exception.class).when(Object)

E.Mockito.when(Object).thenCallRealMethod()

10.Mockito提供的設(shè)置方法執(zhí)行操作的方法有:

A.Mockito.doCallRealMethod().when(Object)

B.Mockito.when(Object).thenCallRealMethod()

C.Mockito.doReturn(value).when(Object)

D.Mockito.doThrow(Exception.class).when(Object)

E.Mockito.when(Object).thenReturn(value)

三、判斷題(每題2分,共10題)

1.Mockito只能在JUnit測試環(huán)境中使用。(×)

2.Mockito可以模擬Java的任何類型的對象,包括內(nèi)部類和匿名類。(√)

3.Mockito的spy方法不會修改對象的行為,只會模擬對象的方法調(diào)用。(√)

4.Mockito的when方法可以用來模擬對象的方法調(diào)用,并設(shè)置返回值。(√)

5.Mockito的verify方法可以用來驗證對象的方法是否被調(diào)用。(√)

6.Mockito的doReturn方法可以用來設(shè)置模擬對象的方法返回值。(√)

7.Mockito的doThrow方法可以用來設(shè)置模擬對象的方法拋出異常。(√)

8.Mockito的verify方法可以用來驗證對象的方法調(diào)用次數(shù)大于等于指定次數(shù)。(√)

9.Mockito的verify方法可以用來驗證對象的方法調(diào)用次數(shù)小于等于指定次數(shù)。(√)

10.Mockito的verify方法可以用來驗證對象的方法從未被調(diào)用過。(√)

四、簡答題(每題5分,共6題)

1.簡述Mockito在Java單元測試中的作用。

2.如何使用Mockito模擬一個接口?

3.如何使用Mockito驗證一個模擬對象的方法是否被調(diào)用?

4.如何使用Mockito設(shè)置一個模擬對象的方法返回特定值?

5.如何使用Mockito設(shè)置一個模擬對象的方法拋出異常?

6.如何使用Mockito驗證一個模擬對象的方法調(diào)用次數(shù)?

試卷答案如下

一、單項選擇題答案及解析:

1.D.Mockito不支持模擬Java的內(nèi)置類,如String、List等

解析:Mockito可以模擬任何類型的對象,包括內(nèi)置類。

2.A.Mockito.mock(Class)

解析:Mockito.mock(Class)用于創(chuàng)建一個模擬對象。

3.A.Mockito.verify(Object,times(int))

解析:Mockito.verify(Object,times(int))用于驗證對象的方法是否被調(diào)用指定次數(shù)。

4.A.Mockito.when(Object).thenReturn(value)

解析:Mockito.when(Object).thenReturn(value)用于設(shè)置模擬對象的方法返回值。

5.A.Mockito.when(Object).thenThrow(Exception.class)

解析:Mockito.when(Object).thenThrow(Exception.class)用于設(shè)置模擬對象的方法拋出異常。

6.A.Mockito.verify(Object,times(int))

解析:Mockito.verify(Object,times(int))用于驗證對象的方法調(diào)用次數(shù)。

7.A.Mockito.doCallRealMethod().when(Object)

解析:Mockito.doCallRealMethod().when(Object)用于設(shè)置模擬對象的方法執(zhí)行實際的方法。

8.A.Mockito.when(Object).thenThrow(Exception.class).thenCallRealMethod().thenReturn(value)

解析:Mockito.when(Object).thenThrow(Exception.class).thenCallRealMethod().thenReturn(value)用于設(shè)置模擬對象的方法先拋出異常,然后執(zhí)行實際的方法,最后返回特定值。

9.A.Mockito.when(Object).thenReturn(value).thenCallRealMethod()

解析:Mockito.when(Object).thenReturn(value).thenCallRealMethod()用于設(shè)置模擬對象的方法返回特定值,然后執(zhí)行實際的方法。

10.A.Mockito.when(Object).thenThrow(Exception.class).thenCallRealMethod().thenReturn(value)

解析:Mockito.when(Object).thenThrow(Exception.class).thenCallRealMethod().thenReturn(value)用于設(shè)置模擬對象的方法先拋出異常,然后執(zhí)行實際的方法,最后返回特定值。

二、多項選擇題答案及解析:

1.A,B,C,D,E

解析:Mockito的優(yōu)點包括提高代碼的可測試性、隔離測試邏輯、提高代碼的復(fù)用性、優(yōu)化開發(fā)效率和降低代碼的復(fù)雜性。

2.A,B,C,D,E

解析:Mockito支持的模擬對象類型包括Java的內(nèi)置類、自定義類、接口、枚舉和數(shù)組。

3.A,B

解析:Mockito主要用于單元測試和集成測試。

4.A,B,C,D,E

解析:Mockito提供的驗證方法包括verify(Object,times(int))、verify(Object,atLeastOnce())、verify(Object,atLeast(int))、verify(Object,atMost(int))和verify(Object,never())。

5.A,B

解析:Mockito提供的設(shè)置返回值的方法包括when(Object).thenReturn(value)和doReturn(value).when(Object)。

6.A,B

解析:Mockito提供的設(shè)置異常拋出的方法包括when(Object).thenThrow(Exception.class)和doThrow(Exception.class).when(Object)。

7.A,B,C,D,E

解析:Mockito提供的驗證調(diào)用順序的方法包括verifyInOrder(Object)、verifyOrder(Object)、verifySequence(Object)、verifySequentially(Object)和verifyInSequence(Object)。

8.A,B,C,D,E

解析:Mockito提供的設(shè)置調(diào)用次數(shù)的方法包括verify(Object,times(int))、verify(Object,atLeastOnce())、verify(Object,atLeast(int))、verify(Object,atMost(int))和verify(Object,never())。

9.A,B,C,D,E

解析:Mockito提供的設(shè)置方法執(zhí)行結(jié)果的方法包括when(Object).thenReturn(value)、doReturn(value).when(Object)、when(Object).thenThrow(Exception.class)、doThrow(Exception.class).when(Object)和when(Object).thenCallRealMethod()。

10.A,B,C,D,E

解析:Mockito提供的設(shè)置方法執(zhí)行操作的方法包括doCallRealMethod().when(Object)、when(Object).thenCallRealMethod()、doReturn(value).when(Object)、doThrow(Exception.class).when(Object)和when(Object).thenReturn(value)。

三、判斷題答案及解析:

1.×

解析:Mockito可以在JUnit、TestNG等測試框架中使用。

2.√

解析:Mockito可以模擬任何類型的對象,包括內(nèi)部類和匿名類。

3.√

解析:Mockito的spy方法不會修改對象的行為,只會模擬對象的方法調(diào)用。

4.√

解析:Mockito的when方法可以用來模擬對象的方法調(diào)用,并設(shè)置返回值。

5.√

解析:Mockito的verify方法可以用來驗證對象的方法是否被調(diào)用。

6.√

解析:Mockito的doReturn方法可以用來設(shè)置模擬對象的方法返回值。

7.√

解析:Mockito的doThrow方法可以用來設(shè)置模擬對

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論