SpringBoot 單元測(cè)試詳解(Mockito、MockBean)
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
? 作者?|? 小蓉蓉Young
來(lái)源 |? urlify.cn/vyaMVn
76套java從入門(mén)到精通實(shí)戰(zhàn)課程分享
一個(gè)測(cè)試方法主要包括三部分:
1)setup
2)執(zhí)行操作
3)驗(yàn)證結(jié)果
public?class?CalculatorTest?{
????Calculator?mCalculator;
?
????@Before?//?setup
????public?void?setup()?{
????????mCalculator?=?new?Calculator();
????}
?
????@Test?//assert?部分可以幫助我們驗(yàn)證一個(gè)結(jié)果
????public?void?testAdd()?throws?Exception?{
????????int?sum?=?mCalculator.add(1,?2);
????????assertEquals(3,?sum);??//為了簡(jiǎn)潔,往往會(huì)static?import?Assert里面的所有方法。
????}
?
????@Test
????@Ignore("not?implemented?yet")?//?測(cè)試時(shí)忽略該方法
????public?void?testMultiply()?throws?Exception?{
????}
?
????//?表示驗(yàn)證這個(gè)測(cè)試方法將拋出?IllegalArgumentException?異常,若沒(méi)拋出,則測(cè)試失敗
????@Test(expected?=?IllegalArgumentException.class)
????public?void?test()?{
????????mCalculator.divide(4,?0);
????}
}
Junit 基本注解介紹
@BeforeClass?在所有測(cè)試方法執(zhí)行前執(zhí)行一次,一般在其中寫(xiě)上整體初始化的代碼。@AfterClass?在所有測(cè)試方法后執(zhí)行一次,一般在其中寫(xiě)上銷(xiāo)毀和釋放資源的代碼。
//?注意這兩個(gè)都是靜態(tài)方法
@BeforeClass
public?static?void?test(){
?????
}
@AfterClass
public?static?void?test(){
}
@Before?在每個(gè)方法測(cè)試前執(zhí)行,一般用來(lái)初始化方法(比如我們?cè)跍y(cè)試別的方法時(shí),類中與其他測(cè)試方法共享的值已經(jīng)被改變,為了保證測(cè)試結(jié)果的有效性,我們會(huì)在@Before注解的方法中重置數(shù)據(jù))@After?在每個(gè)測(cè)試方法執(zhí)行后,在方法執(zhí)行完成后要做的事情。@Test(timeout = 1000)?測(cè)試方法執(zhí)行超過(guò)1000毫秒后算超時(shí),測(cè)試將失敗。@Test(expected = Exception.class)?測(cè)試方法期望得到的異常類,如果方法執(zhí)行沒(méi)有拋出指定的異常,則測(cè)試失敗。@Ignore("not ready yet")?執(zhí)行測(cè)試時(shí)將忽略掉此方法,如果用于修飾類,則忽略整個(gè)類。@Test?編寫(xiě)一般測(cè)試用例用。@RunWith?在 Junit 中有很多個(gè) Runner,他們負(fù)責(zé)調(diào)用你的測(cè)試代碼,每一個(gè) Runner 都有各自的特殊功能,你根據(jù)需要選擇不同的 Runner 來(lái)運(yùn)行你的測(cè)試代碼。
如果我們只是簡(jiǎn)單的做普通 Java 測(cè)試,不涉及 Spring Web 項(xiàng)目,你可以省略?@RunWith?注解,你要根據(jù)需要選擇不同的 Runner 來(lái)運(yùn)行你的測(cè)試代碼。
測(cè)試方法執(zhí)行順序
按照設(shè)計(jì),Junit不指定test方法的執(zhí)行順序。
@FixMethodOrder(MethodSorters.JVM):保留測(cè)試方法的執(zhí)行順序?yàn)镴VM返回的順序。每次測(cè)試的執(zhí)行順序有可能會(huì)所不同。@FixMethodOrder(MethodSorters.NAME_ASCENDING) :根據(jù)測(cè)試方法的方法名排序,按照詞典排序規(guī)則(ASC,從小到大,遞增)。
Failure 是測(cè)試失敗,Error 是程序出錯(cuò)。
測(cè)試方法命名約定
Maven本身并不是一個(gè)單元測(cè)試框架,它只是在構(gòu)建執(zhí)行到特定生命周期階段的時(shí)候,通過(guò)插件來(lái)執(zhí)行JUnit或者TestNG的測(cè)試用例。這個(gè)插件就是maven-surefire-plugin,也可以稱為測(cè)試運(yùn)行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。
在默認(rèn)情況下,maven-surefire-plugin的test目標(biāo)會(huì)自動(dòng)執(zhí)行測(cè)試源碼路徑(默認(rèn)為src/test/java/)下所有符合一組命名模式的測(cè)試類。這組模式為:
*/Test.java:任何子目錄下所有命名以Test開(kāi)關(guān)的Java類。
*/Test.java:任何子目錄下所有命名以Test結(jié)尾的Java類。
*/TestCase.java:任何子目錄下所有命名以TestCase結(jié)尾的Java類。
基于 Spring 的單元測(cè)試編寫(xiě)
首先我們項(xiàng)目一般都是 MVC 分層的,而單元測(cè)試主要是在 Dao 層和 Service 層上進(jìn)行編寫(xiě)。從項(xiàng)目結(jié)構(gòu)上來(lái)說(shuō),Service 層是依賴 Dao 層的,但是從單元測(cè)試角度,對(duì)某個(gè) Service 進(jìn)行單元的時(shí)候,他所有依賴的類都應(yīng)該進(jìn)行Mock。而 Dao 層單元測(cè)試就比較簡(jiǎn)單了,只依賴數(shù)據(jù)庫(kù)中的數(shù)據(jù)。
Mockito
Mockito是mocking框架,它讓你用簡(jiǎn)潔的API做測(cè)試。而且Mockito簡(jiǎn)單易學(xué),它可讀性強(qiáng)和驗(yàn)證語(yǔ)法簡(jiǎn)潔。
Mockito 是一個(gè)針對(duì) Java 的單元測(cè)試模擬框架,它與 EasyMock 和 jMock 很相似,都是為了簡(jiǎn)化單元測(cè)試過(guò)程中測(cè)試上下文 ( 或者稱之為測(cè)試驅(qū)動(dòng)函數(shù)以及樁函數(shù) ) 的搭建而開(kāi)發(fā)的工具
相對(duì)于 EasyMock 和 jMock,Mockito 的優(yōu)點(diǎn)是通過(guò)在執(zhí)行后校驗(yàn)?zāi)男┖瘮?shù)已經(jīng)被調(diào)用,消除了對(duì)期望行為(expectations)的需要。其它的 mocking 庫(kù)需要在執(zhí)行前記錄期望行為(expectations),而這導(dǎo)致了丑陋的初始化代碼。
SpringBoot 中的?pom.xml?文件需要添加的依賴:
????????org.springframework.boot
????????spring-boot-starter-test
????????test
????進(jìn)入?spring-boot-starter-test-2.1.3.RELEASE.pom?可以看到該依賴中已經(jīng)有單元測(cè)試所需的大部分依賴,如:
junit
mockito
hamcrest
若為其他 spring 項(xiàng)目,需要自己添加 Junit 和 mockito 項(xiàng)目。
常用的 Mockito 方法:
| 方法名 | 描述 |
|---|---|
| Mockito.mock(classToMock) | 模擬對(duì)象 |
| Mockito.verify(mock) | 驗(yàn)證行為是否發(fā)生 |
| Mockito.when(methodCall).thenReturn(value1).thenReturn(value2) | 觸發(fā)時(shí)第一次返回value1,第n次都返回value2 |
| Mockito.doThrow(toBeThrown).when(mock).[method] | 模擬拋出異常。 |
| Mockito.mock(classToMock,defaultAnswer) | 使用默認(rèn)Answer模擬對(duì)象 |
| Mockito.when(methodCall).thenReturn(value) | 參數(shù)匹配 |
| Mockito.doReturn(toBeReturned).when(mock).[method] | 參數(shù)匹配(直接執(zhí)行不判斷) |
| Mockito.when(methodCall).thenAnswer(answer)) | 預(yù)期回調(diào)接口生成期望值 |
| Mockito.doAnswer(answer).when(methodCall).[method] | 預(yù)期回調(diào)接口生成期望值(直接執(zhí)行不判斷) |
| Mockito.spy(Object) | 用spy監(jiān)控真實(shí)對(duì)象,設(shè)置真實(shí)對(duì)象行為 |
| Mockito.doNothing().when(mock).[method] | 不做任何返回 |
| Mockito.doCallRealMethod().when(mock).[method] //等價(jià)于Mockito.when(mock.[method]).thenCallRealMethod(); | 調(diào)用真實(shí)的方法 |
| reset(mock) | 重置mock |
示例:
驗(yàn)證行為是否發(fā)生
//模擬創(chuàng)建一個(gè)List對(duì)象
List?mock?=??Mockito.mock(List.class);
//調(diào)用mock對(duì)象的方法
mock.add(1);
mock.clear();
//驗(yàn)證方法是否執(zhí)行
Mockito.verify(mock).add(1);
Mockito.verify(mock).clear();
多次觸發(fā)返回不同值
//mock一個(gè)Iterator類
Iterator?iterator?=?mock(Iterator.class);
//預(yù)設(shè)當(dāng)iterator調(diào)用next()時(shí)第一次返回hello,第n次都返回world
Mockito.when(iterator.next()).thenReturn("hello").thenReturn("world");
//使用mock的對(duì)象
String?result?=?iterator.next()?+?"?"?+?iterator.next()?+?"?"?+?iterator.next();
//驗(yàn)證結(jié)果
Assert.assertEquals("hello?world?world",result);
模擬拋出異常
@Test(expected?=?IOException.class)//期望報(bào)IO異常
public?void?when_thenThrow()?throws?IOException{
??????OutputStream?mock?=?Mockito.mock(OutputStream.class);
??????//預(yù)設(shè)當(dāng)流關(guān)閉時(shí)拋出異常
??????Mockito.doThrow(new?IOException()).when(mock).close();
??????mock.close();
??}使用默認(rèn)Answer模擬對(duì)象
RETURNS_DEEP_STUBS 是創(chuàng)建mock對(duì)象時(shí)的備選參數(shù)之一以下方法deepstubsTest和deepstubsTest2是等價(jià)的
@Test
public?void?deepstubsTest(){
????A?a=Mockito.mock(A.class,Mockito.RETURNS_DEEP_STUBS);
????Mockito.when(a.getB().getName()).thenReturn("Beijing");
????Assert.assertEquals("Beijing",a.getB().getName());
}
?
@Test
public?void?deepstubsTest2(){
????A?a=Mockito.mock(A.class);
????B?b=Mockito.mock(B.class);
????Mockito.when(a.getB()).thenReturn(b);
????Mockito.when(b.getName()).thenReturn("Beijing");
????Assert.assertEquals("Beijing",a.getB().getName());
}
class?A{
????private?B?b;
????public?B?getB(){
????????return?b;
????}
????public?void?setB(B?b){
????????this.b=b;
????}
}
class?B{
????private?String?name;
????public?String?getName(){
????????return?name;
????}
????public?void?setName(String?name){
????????this.name?=?name;
????}
????public?String?getSex(Integer?sex){
????????if(sex==1){
????????????return?"man";
????????}else{
????????????return?"woman";
????????}
????}
}
參數(shù)匹配
@Test
public?void?with_arguments(){
????B?b?=?Mockito.mock(B.class);
????//預(yù)設(shè)根據(jù)不同的參數(shù)返回不同的結(jié)果
????Mockito.when(b.getSex(1)).thenReturn("男");
????Mockito.when(b.getSex(2)).thenReturn("女");
????Assert.assertEquals("男",?b.getSex(1));
????Assert.assertEquals("女",?b.getSex(2));
????//對(duì)于沒(méi)有預(yù)設(shè)的情況會(huì)返回默認(rèn)值
????Assert.assertEquals(null,?b.getSex(0));
}
class?B{
????private?String?name;
????public?String?getName(){
????????return?name;
????}
????public?void?setName(String?name){
????????this.name?=?name;
????}
????public?String?getSex(Integer?sex){
????????if(sex==1){
????????????return?"man";
????????}else{
????????????return?"woman";
????????}
????}
}
匹配任意參數(shù)
Mockito.anyInt()?任何 int 值 ;Mockito.anyLong()?任何 long 值 ;Mockito.anyString()?任何 String 值 ;
Mockito.any(XXX.class)?任何 XXX 類型的值 等等。
@Test
public?void?with_unspecified_arguments(){
????List?list?=?Mockito.mock(List.class);
????//匹配任意參數(shù)
????Mockito.when(list.get(Mockito.anyInt())).thenReturn(1);
????Mockito.when(list.contains(Mockito.argThat(new?IsValid()))).thenReturn(true);
????Assert.assertEquals(1,list.get(1));
????Assert.assertEquals(1,list.get(999));
????Assert.assertTrue(list.contains(1));
????Assert.assertTrue(!list.contains(3));
}
class?IsValid?extends?ArgumentMatcher{
????@Override
????public?boolean?matches(Object?obj)?{
????????return?obj.equals(1)?||?obj.equals(2);
????}
}
注意:使用了參數(shù)匹配,那么所有的參數(shù)都必須通過(guò)matchers來(lái)匹配
Mockito繼承Matchers,anyInt()等均為Matchers方法
當(dāng)傳入兩個(gè)參數(shù),其中一個(gè)參數(shù)采用任意參數(shù)時(shí),指定參數(shù)需要matchers來(lái)對(duì)比
Comparator?comparator?=?mock(Comparator.class);
comparator.compare("nihao","hello");
//如果你使用了參數(shù)匹配,那么所有的參數(shù)都必須通過(guò)matchers來(lái)匹配
Mockito.verify(comparator).compare(Mockito.anyString(),Mockito.eq("hello"));
//下面的為無(wú)效的參數(shù)匹配使用
//verify(comparator).compare(anyString(),"hello");
自定義參數(shù)匹配
@Test
public?void?argumentMatchersTest(){
???//創(chuàng)建mock對(duì)象
???List?mock?=?mock(List.class);
???//argThat(Matches?matcher)方法用來(lái)應(yīng)用自定義的規(guī)則,可以傳入任何實(shí)現(xiàn)Matcher接口的實(shí)現(xiàn)類。
???Mockito.when(mock.addAll(Mockito.argThat(new?IsListofTwoElements()))).thenReturn(true);
???Assert.assertTrue(mock.addAll(Arrays.asList("one","two","three")));
}
class?IsListofTwoElements?extends?ArgumentMatcher
{
???public?boolean?matches(Object?list)
???{
???????return((List)list).size()==3;
???}
}
預(yù)期回調(diào)接口生成期望值
@Test
public?void?answerTest(){
??????List?mockList?=?Mockito.mock(List.class);
??????//使用方法預(yù)期回調(diào)接口生成期望值(Answer結(jié)構(gòu))
??????Mockito.when(mockList.get(Mockito.anyInt())).thenAnswer(new?CustomAnswer());
??????Assert.assertEquals("hello?world:0",mockList.get(0));
??????Assert.assertEquals("hello?world:999",mockList.get(999));
??}
??private?class?CustomAnswer?implements?Answer?{
??????@Override
??????public?String?answer(InvocationOnMock?invocation)?throws?Throwable?{
??????????Object[]?args?=?invocation.getArguments();
??????????return?"hello?world:"+args[0];
??????}
??}
等價(jià)于:(也可使用匿名內(nèi)部類實(shí)現(xiàn))
@Test
?public?void?answer_with_callback(){
??????//使用Answer來(lái)生成我們我們期望的返回
??????Mockito.when(mockList.get(Mockito.anyInt())).thenAnswer(new?Answer預(yù)期回調(diào)接口生成期望值(直接執(zhí)行)
@Test
public?void?testAnswer1(){
List?mock?=?Mockito.mock(List.class);??
??????Mockito.doAnswer(new?CustomAnswer()).when(mock).get(Mockito.anyInt());??
??????Assert.assertEquals("大于三",?mock.get(4));
??????Assert.assertEquals("小于三",?mock.get(2));
}
public?class?CustomAnswer?implements?Answer?{??
??public?String?answer(InvocationOnMock?invocation)?throws?Throwable?{??
??????Object[]?args?=?invocation.getArguments();??
??????Integer?num?=?(Integer)args[0];??
??????if(?num>3?){??
??????????return?"大于三";??
??????}?else?{??
??????????return?"小于三";???
??????}??
??}
}
修改對(duì)未預(yù)設(shè)的調(diào)用返回默認(rèn)期望(指定返回值)
//mock對(duì)象使用Answer來(lái)對(duì)未預(yù)設(shè)的調(diào)用返回默認(rèn)期望值
List?mock?=?Mockito.mock(List.class,new?Answer()?{
?????@Override
?????public?Object?answer(InvocationOnMock?invocation)?throws?Throwable?{
?????????return?999;
?????}
?});
?//下面的get(1)沒(méi)有預(yù)設(shè),通常情況下會(huì)返回NULL,但是使用了Answer改變了默認(rèn)期望值
?Assert.assertEquals(999,?mock.get(1));
?//下面的size()沒(méi)有預(yù)設(shè),通常情況下會(huì)返回0,但是使用了Answer改變了默認(rèn)期望值
?Assert.assertEquals(999,mock.size());
用spy監(jiān)控真實(shí)對(duì)象,設(shè)置真實(shí)對(duì)象行為
???@Test(expected?=?IndexOutOfBoundsException.class)
????public?void?spy_on_real_objects(){
????????List?list?=?new?LinkedList();
????????List?spy?=?Mockito.spy(list);
????????//下面預(yù)設(shè)的spy.get(0)會(huì)報(bào)錯(cuò),因?yàn)闀?huì)調(diào)用真實(shí)對(duì)象的get(0),所以會(huì)拋出越界異常
????????//Mockito.when(spy.get(0)).thenReturn(3);
????????//使用doReturn-when可以避免when-thenReturn調(diào)用真實(shí)對(duì)象api
????????Mockito.doReturn(999).when(spy).get(999);
????????//預(yù)設(shè)size()期望值
????????Mockito.when(spy.size()).thenReturn(100);
????????//調(diào)用真實(shí)對(duì)象的api
????????spy.add(1);
????????spy.add(2);
????????Assert.assertEquals(100,spy.size());
????????Assert.assertEquals(1,spy.get(0));
????????Assert.assertEquals(2,spy.get(1));
????????Assert.assertEquals(999,spy.get(999));
????}不做任何返回
@Test
public?void?Test()?{
????A?a?=?Mockito.mock(A.class);
????//void?方法才能調(diào)用doNothing()
????Mockito.doNothing().when(a).setName(Mockito.anyString());
????a.setName("bb");
????Assert.assertEquals("bb",a.getName());
}
class?A?{
????private?String?name;
????private?void?setName(String?name){
????????this.name?=?name;
????}
????private?String?getName(){
????????return?name;
????}
}
調(diào)用真實(shí)的方法
@Test
public?void?Test()?{
????A?a?=?Mockito.mock(A.class);
????//void?方法才能調(diào)用doNothing()
????Mockito.when(a.getName()).thenReturn("bb");
????Assert.assertEquals("bb",a.getName());
????//等價(jià)于Mockito.when(a.getName()).thenCallRealMethod();
????Mockito.doCallRealMethod().when(a).getName();
????Assert.assertEquals("zhangsan",a.getName());
}
class?A?{
????public?String?getName(){
????????return?"zhangsan";
????}
}
重置 mock
????@Test
????public?void?reset_mock(){
????????List?list?=?mock(List.class);
????????Mockito.?when(list.size()).thenReturn(10);
????????list.add(1);
????????Assert.assertEquals(10,list.size());
????????//重置mock,清除所有的互動(dòng)和預(yù)設(shè)
????????Mockito.reset(list);
????????Assert.assertEquals(0,list.size());
????}
@Mock?注解
public?class?MockitoTest?{
????@Mock
????private?List?mockList;
????//必須在基類中添加初始化mock的代碼,否則報(bào)錯(cuò)mock的對(duì)象為NULL
????public?MockitoTest(){
????????MockitoAnnotations.initMocks(this);
????}
????@Test
????public?void?AnnoTest()?{
????????????mockList.add(1);
????????Mockito.verify(mockList).add(1);
????}
}
指定測(cè)試類使用運(yùn)行器:MockitoJUnitRunner
@RunWith(MockitoJUnitRunner.class)
public?class?MockitoTest2?{
????@Mock
????private?List?mockList;
????@Test
????public?void?shorthand(){
????????mockList.add(1);
????????Mockito.verify(mockList).add(1);
????}
}@MockBean
使用?@MockBean?可以解決單元測(cè)試中的一些依賴問(wèn)題,示例如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public?class?ServiceWithMockBeanTest?{
????@MockBean
????SampleDependencyA?dependencyA;
????@Autowired
????SampleService?sampleService;
????@Test
????public?void?testDependency()?{
????????when(dependencyA.getExternalValue(anyString())).thenReturn("mock?val:?A");
????????assertEquals("mock?val:?A",?sampleService.foo());
????}
}@MockBean?只能 mock 本地的代碼——或者說(shuō)是自己寫(xiě)的代碼,對(duì)于儲(chǔ)存在庫(kù)中而且又是以 Bean 的形式裝配到代碼中的類無(wú)能為力。
@SpyBean?解決了 SpringBoot 的單元測(cè)試中?@MockBean?不能 mock 庫(kù)中自動(dòng)裝配的 Bean 的局限(目前還沒(méi)需求,有需要的自己查閱資料)。
粉絲福利:Java從入門(mén)到入土學(xué)習(xí)路線圖
??????

??長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
