欢迎点击头像进入主页查看更多内容....
JUnitGenerator安装
在Idea市场里安装JUnitGenerator插件如图,重启后生效
设置生成路径及模板
Output Path: ${SOURCEPATH}/..//../test/java/${PACKAGE}/${FILENAME}
注:根据实际项目结构调整,设置为默认模板,另需要在Project Structure-->Modules中设置Test文件目录,可改造为自启动,随容器启动,
设置Junit4 生成模板
支持 CommonResponse 或 Wrapper
#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end
#foreach ($entry in $entryList)
#set( $testClass="${entry.className}Test")
package $entry.packageName;
import java.util.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.springframework.http.ResponseEntity;
/**
* BSF+Junit4
* @Creator zhaokk
* @CreateDate ${Date}
* @DESC
*/
@SuppressWarnings("all")
public class $testClass {
public final String ipAndPort="localhost:8080/";
public final String token="login-token";
public final String extend = "extend";
private static HttpHeaders requestHeaders;
@Before
public void before() throws Exception {
requestHeaders = new HttpHeaders();
requestHeaders.add("token", token);
requestHeaders.add("", "");
JunitUtils.setRequestHeaders(requestHeaders);
}
@After
public void after() throws Exception {
}
#foreach($method in $entry.methodList)
/**
*
* Method: $method.signature
*
*/
@Test
public void test#cap(${method.name})() throws Exception {
//Make request
TestEntity testEntity=JunitUtils.checkRequest(ipAndPort,token);
//Show Controller & Mehtod
//FIXME new $entry.packageName.${entry.className}().${method.name}();
//Model 1 (Parameter is url)
//Map<String,String> objectRequest=new HashMap();
//objectRequest.put("{id}","1000000157729");
//testEntity.setRequestObj(objectRequest);
//Model 2 (Parameter is Object)
//UserLoginReq objectRequest = new UserLoginReq();//JSON.parseObject("jsonString",UserLoginReq.class)
//objectRequest.setUsername("18311140968");
//objectRequest.setPassword("aa123456");
//testEntity.setRequestObj(objectRequest);
//Model 3 (Parameter is url & Object)
//Map<String,String> urlMap=new HashMap();
//urlMap.put("{pageNo}","1");
//urlMap.put("{pageSize}","10");
//UserLoginReq objectRequest = new UserLoginReq();//JSON.parseObject("jsonString",UserLoginReq.class)
//objectRequest.setUsername("18311140968");
//objectRequest.setPassword("aa123456");
//testEntity.setRequestObj(objectRequest);
//model 1 and model 2 (ResponseEntity)
ResponseEntity<CommonResponse> responseResponseEntity=JunitUtils.getResponseEntity(testEntity);
//model 3 (ResponseEntity)
//ResponseEntity<CommonResponse> responseResponseEntity=JunitUtils.getResponseEntity(urlMap,testEntity);
//ResponseEntity To Object
//CommonResponse<UserResp> response = responseResponseEntity.getBody();
//Check CommonResponse code is 200000
JunitUtils.checkResponse(responseResponseEntity.getBody());
}
#end
#foreach($method in $entry.privateMethodList)
/**
*
* Method: $method.signature
*
*/
@Test
public void test#cap(${method.name})() throws Exception {
//TODO: Test goes here...
#foreach($string in $method.reflectionCode)
$string
#end
}
#end
}
#end
代码块1:根据ipAndPort和token 拼装基础的URL和请求头
代码块2:工具块,方便快速跳转到本方法的源头
代码块3:模式1参数封装,一般用于GET 和DELETE 请求,纯URL
代码块4:模式2参数封装,一般用于POST和PUT 请求,纯对象。支持Json字符串,如下:
UpdateUserByPKReq objectRequest = JunitUtils.jsonToObject("{\"name\":\"接口测试-勿删\",\"userNumber\":\"1573195928331\"",UpdateUserByPKReq.class);
代码块5:模式1和模式2的组合,拼装URL+对象
代码块6:适用于模式1和模式2,进行具体的请求并获得响应结果
代码块7:适用于模式3,进行具体的请求并获得响应结果
代码块8:检查响应结果,默认200000为成功,如果非200000失败。如有多个状态码均为成功,可以增加成功状态码进行入参,
如下:JunitUtils.checkResponse(responseResponseEntity.getBody(),600220)
这里可自行改造为
/**
* @author zhaokk
* @create ${Date}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = com.xxx.class)
@Slf4j
workspace.xml中加入
<component name="PropertiesComponent">
<property name="dynamic.classpath" value="true" />
<!-- -->