• Mockito (八)


    Mockito spy 和 @Spy 注解

    spy 和 mock不同,不同点是:

    • spy 的参数是对象实例,mock 的参数是 class。
    • 被 spy 的对象,调用其方法时默认会走真实方法。mock 对象不会。

    下面是一个对比:

    import org.junit.Assert;
    import org.junit.Test;
    import static org.mockito.Mockito.*;
    
    
    class ExampleService {
    
        int add(int a, int b) {
            return a+b;
        }
    
    }
    
    public class MockitoDemo {
    
        // 测试 spy
        @Test
        public void test_spy() {
    
            ExampleService spyExampleService = spy(new ExampleService());
    
            // 默认会走真实方法
            Assert.assertEquals(3, spyExampleService.add(1, 2));
    
            // 打桩后,不会走了
            when(spyExampleService.add(1, 2)).thenReturn(10);
            Assert.assertEquals(10, spyExampleService.add(1, 2));
    
            // 但是参数不匹配的调用,依然走真实方法
            Assert.assertEquals(3, spyExampleService.add(2, 1));
    
        }
    
        // 测试 mock
        @Test
        public void test_mock() {
    
            ExampleService mockExampleService = mock(ExampleService.class);
    
            // 默认返回结果是返回类型int的默认值
            Assert.assertEquals(0, mockExampleService.add(1, 2));
    
        }
    
    
    }

    spy 对应注解 @Spy,和 @Mock 是一样用的。

    import org.junit.Assert;
    import org.junit.Test;
    import org.mockito.MockitoAnnotations;
    import org.mockito.Spy;
    
    import static org.mockito.Mockito.*;
    
    
    class ExampleService {
    
        int add(int a, int b) {
            return a+b;
        }
    
    }
    
    public class MockitoDemo {
    
        @Spy
        private ExampleService spyExampleService;
    
        @Test
        public void test_spy() {
    
            MockitoAnnotations.initMocks(this);
    
            Assert.assertEquals(3, spyExampleService.add(1, 2));
    
            when(spyExampleService.add(1, 2)).thenReturn(10);
            Assert.assertEquals(10, spyExampleService.add(1, 2));
    
        }
    
    }

    对于@Spy,如果发现修饰的变量是 null,会自动调用类的无参构造函数来初始化。

    所以下面两种写法是等价的:

    // 写法1
    @Spy
    private ExampleService spyExampleService;
    
    // 写法2
    @Spy
    private ExampleService spyExampleService = new ExampleService();

    如果没有无参构造函数,必须使用写法2。例子:

    import org.junit.Assert;
    import org.junit.Test;
    import org.mockito.MockitoAnnotations;
    import org.mockito.Spy;
    
    class ExampleService {
    
        private int a;
    
        public ExampleService(int a) {
            this.a = a;
        }
    
        int add(int b) {
            return a+b;
        }
    
    }
    
    public class MockitoDemo {
    
        @Spy
        private ExampleService spyExampleService = new ExampleService(1);
    
        @Test
        public void test_spy() {
    
            MockitoAnnotations.initMocks(this);
    
            Assert.assertEquals(3, spyExampleService.add(2));
    
        }
    
    }

    转载:https://www.letianbiji.com/java-mockito/mockito-spy.html

    带着疑问去思考,然后串联,进而归纳总结,不断追问自己,进行自我辩证,像侦查嫌疑案件一样看待技术问题,漆黑的街道,你我一起寻找线索,你就是技术界大侦探福尔摩斯
  • 相关阅读:
    Zabbix,Nagios,OneAPM Servers 安装部署大比拼
    Android 手把手带你玩转自己定义相机
    Sublime Text3 快捷键
    超具体Windows版本号编译执行React Native官方实例UIExplorer项目(多图慎入)
    poj 1664 放苹果(递推)
    在HyperLedger Fabric中启用CouchDB作为State Database
    HyperLedger Fabric 1.0的Transaction处理流程
    如何将Bitcoin比特币区块链数据导入关系数据库
    在Ubuntu中部署并测试Fabric 1.0 Beta
    在Ubuntu上快速搭建基于Beego的RESTful API
  • 原文地址:https://www.cnblogs.com/cainiao-Shun666/p/14804829.html
Copyright © 2020-2023  润新知