日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Maven简单的配置Junit测试及使用简单的mock

發(fā)布時(shí)間:2025/5/22 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven简单的配置Junit测试及使用简单的mock 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、maven依賴配置如下

<dependency><groupId>org.mockito</groupId><artifactId>mockito-all</artifactId><version>1.9.5</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><build><plugins><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><appendAssemblyId>false</appendAssemblyId><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><mainClass>RunJob</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>assembly</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.5</version></plugin></plugins></build>

?

?

2、編寫測(cè)試代碼

public class SimpleTest {@Testpublic void test() {Assert.assertTrue("a".equals("a"));}@Testpublic void testMock() {List m = mock(List.class);m.add(1);m.clear();verify(m).add(1);//驗(yàn)證執(zhí)行過add(1)操作verify(m).clear();//驗(yàn)證執(zhí)行過clear操作 }@Testpublic void when_thenReturn() {//mock一個(gè)Iterator類Iterator iterator = mock(Iterator.class);//預(yù)設(shè)當(dāng)iterator調(diào)用next()時(shí)第一次返回hello,第n次都返回worldwhen(iterator.next()).thenReturn("hello").thenReturn("world");//使用mock的對(duì)象String result = iterator.next() + " " + iterator.next() + " " + iterator.next();//驗(yàn)證結(jié)果assertEquals("hello world world", result);} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/maxiaofang/p/6608081.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Maven简单的配置Junit测试及使用简单的mock的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。