zoukankan      html  css  js  c++  java
  • 单元测试Mockito中的Mock和Spy

    转载:https://blog.csdn.net/qq_30141957/article/details/81273829

    项目中,有些函数需要处理某个服务的返回结果,而在对函数单元测试的时候,又不能启动那些服务,这里就可以利用Mockito工具。Mockito中的Mock和Spy都可用于拦截那些尚未实现或不期望被真实调用的对象和方法,并为其设置自定义行为。二者的区别在于:

    1、Mock声明的对象,对函数的调用均执行mock(即虚假函数),不执行真正部分。

    2、Spy声明的对象,对函数的调用均执行真正部分。

    例:

    1.  
      public class Main {
    2.  
       
    3.  
      public void fun(String s) {
    4.  
      System.out.println(s + " : fun");
    5.  
      fun1(s);
    6.  
      fun2(s);
    7.  
      }
    8.  
       
    9.  
      public void fun1(String s) {
    10.  
      System.out.println(s + " : fun1");
    11.  
      }
    12.  
       
    13.  
      private void fun2(String s) {
    14.  
      System.out.println(s + " : fun2");
    15.  
      }
    16.  
       
    17.  
      public int getVal(){
    18.  
      return 5;
    19.  
      }
    20.  
      }

    Mock使用实例

    1、使用doCallRealMethod().when()调用函数真正部分。

    2、使用when().thenReturn自定义函数返回值。

    1.  
      import static org.mockito.ArgumentMatchers.anyString;
    2.  
      import static org.mockito.Mockito.doCallRealMethod;
    3.  
      import static org.mockito.Mockito.when;
    4.  
       
    5.  
      import org.junit.Before;
    6.  
      import org.junit.Test;
    7.  
      import org.mockito.Mock;
    8.  
      import org.mockito.MockitoAnnotations;
    9.  
       
    10.  
      public class MainTest {
    11.  
       
    12.  
      @Mock
    13.  
      Main mockMain;
    14.  
       
    15.  
      @Before
    16.  
      public void init() {
    17.  
      MockitoAnnotations.initMocks(this);
    18.  
      }
    19.  
       
    20.  
      @Test
    21.  
      public void testFun() {
    22.  
      // 执行mock,而不是真正部分,所以没有打印任何信息
    23.  
      mockMain.fun("mock test One");
    24.  
       
    25.  
      // doCallRealMethod声明后,执行真正部分
    26.  
      // 但是Mock只能对public(fun1)和protected函数进行mock
    27.  
      // 对private函数(fun2)仍执行真正部分
    28.  
      // 所以输出fun和fun2
    29.  
      doCallRealMethod().when(mockMain).fun(anyString());
    30.  
      mockMain.fun("mock test Two");
    31.  
       
    32.  
      // 执行mock,输出int的默认值0,而不是5
    33.  
      System.out.println("val: " + mockMain.getVal());
    34.  
      // when声明后,既不走真正部分,也不走mock,直接返回thenReturn()中定义的值
    35.  
      // 注意:该值的类型需要和when中函数返回值类型一致
    36.  
      when(mockMain.getVal()).thenReturn(10);
    37.  
      System.out.println("val: " + mockMain.getVal());
    38.  
      }
    39.  
       
    40.  
      }

    Spy使用实例

    1、使用when().thenReturn自定义函数返回值。

    1.  
      import static org.mockito.Mockito.when;
    2.  
       
    3.  
      import org.junit.Before;
    4.  
      import org.junit.Test;
    5.  
      import org.mockito.MockitoAnnotations;
    6.  
      import org.mockito.Spy;
    7.  
       
    8.  
      public class MainTest {
    9.  
       
    10.  
      @Spy
    11.  
      Main spyMain;
    12.  
       
    13.  
      @Before
    14.  
      public void init() {
    15.  
      MockitoAnnotations.initMocks(this);
    16.  
      }
    17.  
       
    18.  
      @Test
    19.  
      public void testFun() {
    20.  
      // 执行真正部分
    21.  
      spyMain.fun("mock test One");
    22.  
       
    23.  
      // 执行真正部分
    24.  
      System.out.println("val: " + spyMain.getVal());
    25.  
      // 自定义返回值
    26.  
      when(spyMain.getVal()).thenReturn(10);
    27.  
      System.out.println("val: " + spyMain.getVal());
    28.  
      }
    29.  
      }


    参考文章:

    1、mockito中实现部分mock两种方式

    2、@mock和@spy在mock私有方法的区别,使用@spy模拟私有方法进行测试时sonar统计是有覆盖率的

    3、Mockito的参数匹配

  • 相关阅读:
    231. Power of Two
    204. Count Primes
    205. Isomorphic Strings
    203. Remove Linked List Elements
    179. Largest Number
    922. Sort Array By Parity II
    350. Intersection of Two Arrays II
    242. Valid Anagram
    164. Maximum Gap
    147. Insertion Sort List
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/9546499.html
Copyright © 2011-2022 走看看