单元测试—IDEA+Junit5+JSON

前言

单元测试是开发自验必不可少的一环,IDEA本身已经默认集成单元测试的插件,不用再额外安装插件,但是Junit5目前只支持CSV格式的参数注入,此处在前人JSON数据驱动的基础上,支持json文件多参数注入。

因为我这边的测试更偏向于多json文件参数组装复用,所以JsonFileArgumentsProvider.java代码略有变动。

Maven依赖

        
            junit
            junit
            4.12
            test
        
        
            org.junit.jupiter
            junit-jupiter
            RELEASE
            test
        
        
            org.junit.jupiter
            junit-jupiter-params
            5.7.0
            compile
        
        
            org.springframework
            spring-test
            5.2.9.RELEASE
        

路径目录

路径目录

此处的junitJson文件用于扩展Junit支持JSON格式的参数注入,直接下载后贴在各自的项目中就行,我这边的单元测试不作提交,所以扩展的代码也就放在test类了,其中resources目录用来存放json文件

JsonFileArgumentsProvider.java支持多JSON文件组装参数注入

package junitJson;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.support.AnnotationConsumer;
import org.junit.platform.commons.util.Preconditions;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.stream.Stream;

public class JsonFileArgumentsProvider implements ArgumentsProvider, AnnotationConsumer {

    private final BiFunction inputStreamProvider;

    private String[] resources;

    public JsonFileArgumentsProvider() throws Exception {
        this(Class::getResourceAsStream);
    }

    JsonFileArgumentsProvider(BiFunction inputStreamProvider) {
        this.inputStreamProvider = inputStreamProvider;
    }

    private static Object values(InputStream inputStream) {
        Object jsonObject = null;
        try {
            jsonObject = JSONObject.parse(IOUtils.toString(inputStream, "utf8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    static Stream getObjectStream(Object jsonObject) {
        if (jsonObject instanceof JSONArray) {
            return ((JSONArray) jsonObject).stream();
        } else if (jsonObject instanceof JSONObject) {
            return Stream.of(jsonObject);
        }
        return null;
    }

    @Override
    public void accept(JsonFileSource jsonFileSource) {
        resources = jsonFileSource.resources();
    }

    @Override
    public Stream provideArguments(ExtensionContext context) {
        Object[] objects = Arrays.stream(resources)
            .map(resource -> openInputStream(context, resource))
            .map(JsonFileArgumentsProvider::values)
            .toArray();
        return Stream.of(Arguments.of(objects));
    }

    private InputStream openInputStream(ExtensionContext context, String resource) {
        Preconditions.notBlank(resource, "Classpath resource [" + resource + "] must not be null or blank");
        Class testClass = context.getRequiredTestClass();
        return Preconditions.notNull(inputStreamProvider.apply(testClass, resource),
            () -> "Classpath resource [" + resource + "] does not exist");
    }
}

 
 

IDEA生成单元测试类

Alt + Insert

第一步

勾选生成

生成测试类demo

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = CalculationEntry.class)
@DisplayName("IPCA场景覆盖测试类")
class CalculationEntryTest {

    @Autowired
    private CalculationEntry calculationEntry;

    @DisplayName("A->B->C单链路全数据场景测试")
    @ParameterizedTest
    @JsonFileSource(resources = {"/singleResult.json", "/singleResultBack.json","/deviceMacList.json"})
    void singleLinkFullData(JSONArray ipcaNodeInfoList, JSONArray ipcaNodeInfoBackList, JSONArray deviceMacList) {
        System.out.println(ipcaNodeInfoList);
    }
}
  • Junit5使用@ExtendWith(SpringExtension.class)实现和spring的互动
  • @ContextConfiguration使用classes参数按需注入所用的类,避免直接路径注入XML其他启动类报错

参考链接

  1. Junit 5官方文档中文版

  2. Junit5加上json数据源

你可能感兴趣的:(单元测试—IDEA+Junit5+JSON)