使用mockito辅助springboot service层单元测试

 

目的

        测试一个springboot service层的一段插入MySQL代码性能;

要求

        1. 单元测试,不走web调用;

        2. 数据来自第三方服务,数据量较大,返回时间10s+,希望mock掉此过程;

方案

        junit+mockito

代码

?下面的类是要测的service,startCalculation()函数new thread新开线程(错误做法)里的“批量插入data”

是要测试的代码,dataScienceRpc.callMethod()是插入数据的来源,也是要通过mock回避掉的过程:

@Service
public class ExampleServiceImpl implements ExampleService {

    // 其它变量&函数
    @Autowired
    private DataScienceRpc dataScienceRpc;

    @Override
    public boolean startCalculation(long id) {

        // 其它操作
        Runnable myRunnable = () -> {
            // 其它操作
            JSONArray config = JSONUtils.toJSONArray(str);
            JSONArray data = dataScienceRpc.callMethod(id, config);
            // 批量插入data
        };

        Thread thread = new Thread(myRunnable);
        thread.start();

    }

}

?下面是springboot里pom.xml对junit+mockito的配置(springboot自带mockito):

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.0.6.RELEASE</version>
    <!--<scope>test</scope>-->
</dependency>

 ?下面是单元测试代码:

import com.alibaba.fastjson.JSONArray;
import com.example.DataScienceRpc;
import com.example.ExampleService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.*;
import java.util.ArrayList;

import static org.mockito.Mockito.when;

/**
 * 必须和springboot启动类放在同一命名空间下
 */

// 使用这个跑单测会加载整个容器
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ExampleTest {

    @MockBean
    private DataScienceRpc dataScienceRpc;

    @Autowired
    ExampleService exampleService;


    @Before
    public void setup(){
        String dataPath = "/Desktop/data.txt";
        String data = readToBuffer(dataPath);
        JSONArray jsonArr = JSONArray.parseArray(data);

        when(dataScienceRpc.callMethod(Mockito.anyLong(), Mockito.any(JSONArray.class)))
                .thenReturn(jsonArr);

    }

    @Test
    public void test(){

        exampleService.startCalculation(133);
    }

    public String readToBuffer(String filePath){
        // 读取文件
    }

}

说明(坑)

        1.  测试类必须和springboot启动类放在同一命名空间下;

        2. @RunWith(SpringJUnit4ClassRunner.class),@SpringBootTest注解,加载整个容器;

        3. @MockBean,将mock出spring bean,若与spring容器无关,可用@Mock;

        4. Mockito.when方法mock函数返回值,不会进入该函数内部;若使用Mockito.anyLong()等方法模糊匹配函数的任意此类型参数,注意当运行时函数参数类型与指定Mockito类型不符,该mock将不会成功;

        此博文不错:https://www.cnblogs.com/ceshi2016/p/9550823.html

        5. 此情况下startCalculation()函数执行完后立即返回,new出的thread(异步执行)没执行完,JVM就会退出,导致要测试的代码执行不完;故不建议此种写法,异步执行的过程应该抽象出一个函数,一是保持上层函数简洁,可读性强,二是方便单元测试;

 

 

 

 

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值