在SpringBoot中编写Mock单元测试

1. 新建测试类

在IDEA中写好的类中,右建GO TO,选择TEST,如果没有对应的单元测试类就选择创建。

在创建该类的单元测试类前,先讲一下这几个基本注解:

@Test:使用该注解标注的public void方法会表示为一个测试方法;
@BeforeClass:表示在类中的任意public static void方法执行之前执行;
@AfterClass:表示在类中的任意public static void方法之后执行;
@Before:表示在任意使用@Test注解标注的public void方法执行之前执行;
@After:表示在任意使用@Test注解标注的public void方法执行之后执行;

2. 添加依赖

新建的springBoot项目中默认包含了spring-boot-starter-test的依赖,如果没有包含可自行在pom.xml中添加依赖。

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
 </dependency>

3. Mock的概念

所谓的mock就是创建一个类的虚假的对象,在测试环境中,用来替换掉真实的对象,以达到两大目的:

  1. 验证这个对象的某些方法的调用情况,调用了多少次,参数是什么等等
  2. 指定这个对象的某些方法的行为,返回特定的值,或者是执行特定的动作

使用Mock之前,需要在@Before或@BeforeClass对应的方法中添加如下,表示添加mock注解初始化。

MockitoAnnotations.initMocks(this);

另外需要补充以下几个常用的测试注解:

  • @InjectMocks:通过创建一个实例,它可以调用真实代码的方法,其余用@Mock(或@Spy)注解创建的mock将被注入到用该实例中。

  • @Mock:对函数的调用均执行mock(即虚假函数),不执行真正部分。

  • @Spy:对函数的调用均执行真正部分。

Mockito中的Mock和Spy都可用于拦截那些尚未实现或不期望被真实调用的对象和方法,并为其设置自定义行为。二者的区别在于Mock不真实调用,Spy会真实调用。

  • @mock与@spy使用示例

(1)被测试类

class ExampleService {
  
    int add(int a, int b) {
        return a+b;
    }
  
}

(2)测试类

import org.junit.Assert;
import org.junit.Test;
import static org.mockito.Mockito.*;

public class MockitoDemo {
    @Spy
    private ExampleService spyExampleService;
  
    @Mock
    private ExampleService mockExampleService;

    // 测试 spy
    @Test
    public void test_spy() {
      
        // 默认会走真实方法
        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() {

        // 默认返回结果是返回类型int的默认值
        Assert.assertEquals(0, mockExampleService.add(1, 2));

    }
}
  • @InjectMock和@Mock使用示例

(1)被测试类

class MockExampleService {
    
    int add(int a, int b) {
        return a+b;
    }
  
}

class InjectMockExampleService {
    
    @Autowired
    private MockExampleService mockExampleService;
      
    int add(int a, int b) {
        return mockExampleService.add(a, b);
    }
  
}

(2)测试类

import org.junit.Assert;
import org.junit.Test;
import static org.mockito.Mockito.*;

public class MockitoDemo {
    @InjectMock
    private InjectMockExampleService injectMockExampleService;
  
    @Mock
    private MockExampleService mockExampleService;
    
    // 初始化汉顺
    @Before
    public void init() throw Exception {
            MockitoAnnotations.initMocks(this);
    }

    // 使用Mockito模拟
    @Test
    public void test() {
      
        // 模拟MockExampleServiceadd函数对于任何参数返回都为10
                when(mockExampleService.add(anyInt(), anyInt())).thenReturn(10);
        // InjectMock会走真实的add方法,只不过mock会返回一个模拟的结果
        Assert.assertEquals(10, injectMockExampleService.add(1, 2));

    }
}

4. 常用的 Mockito 方法

Mockito的使用,一般有以下几种组合:参考链接

  • do/when:包括doThrow(…).when(…)/doReturn(…).when(…)/doAnswer(…).when(…)
  • given/will:包括given(…).willReturn(…)/given(…).willAnswer(…)
  • when/then: 包括when(…).thenReturn(…)/when(…).thenAnswer(…)/when(…).thenThrow(…)

Mockito 有多种匹配函数,部分如下:

函数名 匹配类型
any() 所有对象类型
anyInt() 基本类型 int、非 null 的 Integer 类型
anyChar() 基本类型 char、非 null 的 Character 类型
anyShort() 基本类型 short、非 null 的 Short 类型
anyBoolean() 基本类型 boolean、非 null 的 Boolean 类型
anyDouble() 基本类型 double、非 null 的 Double 类型
anyFloat() 基本类型 float、非 null 的 Float 类型
anyLong() 基本类型 long、非 null 的 Long 类型
anyByte() 基本类型 byte、非 null 的 Byte 类型
anyString() String 类型(不能是 null)
anyList() List<T> 类型(不能是 null)
anyMap() Map<K, V>类型(不能是 null)

5. 参考

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容