junit5与junit4区别比较大,junit5使用了大量的jdk8特性,lambda表达式,使用junit5前需要自行脑补jdk8特性
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
spring boot2.x使用junit5需要使用注解 @ExtendWith(SpringExtension.class),下面的例子使用junit5的参数化测试的功能,即配置测试参数数据源,这里我没有使用junit5的 csv数据源,我采用了自定义扩展数据源方式,使用json数据源的方式。
package com.xxx.junit5.test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import com.alibaba.fastjson.JSONObject;
import com.xxx.spring.SpringBootStarter;
import com.xxx