版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
小程序SringBoot接口项目测试类写法
话不多说,直接上代码:
//创建一个接口测试类
public class PersonInfoActionTest {
//写一个main方法
public static void main(String[] args){
//创建测试类对象
PersonInfoActionTest test=new PersonInfoActionTest();
//调用测试方法
test.testGoMyself();
}
//创建测试方法
private static void testGoMyself(){
PostMethod post=null;//post请求方式
try {
HttpClient client = new HttpClient();
post=new PostMethod("http://localhost:8081/myself/goMyself");//填写各自需要测试的请求地址
NameValuePair pair = new NameValuePair("userId", "1");//参数可以有n多个
post.setRequestBody(new NameValuePair[]{pair});//多个参数是添加方式如{pair,pair1,pair2......,n}
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");;
double startTime = System.currentTimeMillis();//测试开始时间(为测试运行时长)
int status = client.executeMethod(post);//测试状态
double endTime = System.currentTimeMillis();//测试结束时间
System.out.println(post.getResponseBodyAsString());//输出响应参数
//结果输出
System.out.println("测试时长:"+(endTime-startTime)/1000+"秒");
System.out.println("测试状态:"+(status));
}catch (Exception e){
e.printStackTrace();
}finally {
post.releaseConnection();
}
}
}
完成,接口测试类也有好多种,这是我常用的一种分享给大家做做参考