<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          spring單元測(cè)試之Mockito

          共 10278字,需瀏覽 21分鐘

           ·

          2021-06-16 12:57

          Mockito 是一個(gè)針對(duì) Java 的單元測(cè)試模擬框架,它與 EasyMock 和 jMock 很相似,都是為了簡(jiǎn)化單元測(cè)試過(guò)程中測(cè)試上下文 ( 或者稱之為測(cè)試驅(qū)動(dòng)函數(shù)以及樁函數(shù) ) 的搭建而開發(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 文件需要添加的依賴:

              <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
          </dependency>
          復(fù)制代碼

          進(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<Integer> 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ù)制代碼

          多次觸發(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);
          復(fù)制代碼

          模擬拋出異常

          @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();
          }
          復(fù)制代碼

          使用默認(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";
          }
          }
          }
          復(fù)制代碼

          參數(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ì)于沒有預(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";
          }
          }
          }
          復(fù)制代碼

          匹配任意參數(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<List>{
          @Override
          public boolean matches(Object obj) {
          return obj.equals(1) || obj.equals(2);
          }
          }
          復(fù)制代碼

          注意:使用了參數(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");
          復(fù)制代碼

          自定義參數(shù)匹配

          @Test
          public void argumentMatchersTest(){
          //創(chuàng)建mock對(duì)象
          List<String> mock = mock(List.class);
          //argThat(Matches<T> 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<List>
          {
          public boolean matches(Object list)
          {
          return((List)list).size()==3;
          }
          }
          復(fù)制代碼

          預(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<String> {
          @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<Object>() {
          @Override
          public Object answer(InvocationOnMock invocation) throws Throwable {
          Object[] args = invocation.getArguments();
          return "hello world:"+args[0];
          }
          });
          Assert.assertEquals("hello world:0",mockList.get(0));
          Assert. assertEquals("hello world:999",mockList.get(999));
          }
          復(fù)制代碼

          預(yù)期回調(diào)接口生成期望值(直接執(zhí)行)

          @Test
          public void testAnswer1(){
          List<String> 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<String> {
          public String answer(InvocationOnMock invocation) throws Throwable {
          Object[] args = invocation.getArguments();
          Integer num = (Integer)args[0];
          if( num>3 ){
          return "大于三";
          } else {
          return "小于三";
          }
          }
          }
          復(fù)制代碼

          修改對(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)沒有預(yù)設(shè),通常情況下會(huì)返回NULL,但是使用了Answer改變了默認(rèn)期望值
          Assert.assertEquals(999, mock.get(1));
          //下面的size()沒有預(yù)設(shè),通常情況下會(huì)返回0,但是使用了Answer改變了默認(rèn)期望值
          Assert.assertEquals(999,mock.size());
          復(fù)制代碼

          用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));
          }
          復(fù)制代碼

          不做任何返回

          @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;
          }
          }
          復(fù)制代碼

          調(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";
          }
          }
          復(fù)制代碼

          重置 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());
          }
          復(fù)制代碼

          @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);
          }
          }
          復(fù)制代碼

          使用 @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());
          }
          }
          復(fù)制代碼

          @MockBean 只能 mock 本地的代碼——或者說(shuō)是自己寫的代碼,對(duì)于儲(chǔ)存在庫(kù)中而且又是以 Bean 的形式裝配到代碼中的類無(wú)能為力。

          @SpyBean 解決了 SpringBoot 的單元測(cè)試中 @MockBean 不能 mock 庫(kù)中自動(dòng)裝配的 Bean 的局限(目前還沒需求,有需要的自己查閱資料)。


          作者:蔣老濕
          鏈接:https://juejin.cn/post/6973679299500965896
          來(lái)源:掘金
          著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。



          瀏覽 65
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天做夜夜操 | 欧美自拍视频 | 国产精品久久久久久久久久小说 | 北条麻妃一区二区三区在线播放 | 国产欧美一区二区三区精品酒店 |