前言
单元测试是开发自验必不可少的一环,IDEA本身已经默认集成单元测试的插件,不用再额外安装插件,但是Junit5目前只支持CSV格式的参数注入,此处在前人JSON数据驱动的基础上,支持json文件多参数注入。
因为我这边的测试更偏向于多json文件参数组装复用,所以JsonFileArgumentsProvider.java代码略有变动。
Maven依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
路径目录
路径目录此处的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<JsonFileSource> {
private final BiFunction<Class, String, InputStream> inputStreamProvider;
private String[] resources;
public JsonFileArgumentsProvider() throws Exception {
this(Class::getResourceAsStream);
}
JsonFileArgumentsProvider(BiFunction<Class, String, InputStream> 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<Object> 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<? extends Arguments> 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其他启动类报错
网友评论