使用httpclient进行接口的测试调用
1.pom.xml中增加如下配置:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
2、文件上传接口测试
/**
* 上传文件接口
*/
@Test(description = "上传文件接口")
public void testFileUpload() throws IOException {
String url="http://localhost:8090/fileUpload";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
MultipartPostMethod multipartPostMethod=new MultipartPostMethod(url);
try {
multipartPostMethod.addParameter("file", new File("D:\\CKW_Simple.log"));
multipartPostMethod.setRequestHeader("Content-Type", "multipart/form-data");
int status = httpClient.executeMethod(multipartPostMethod);
String responsBody = multipartPostMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responsBody:" + responsBody);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
3、增加学生接口
/**
* 增加学生接口
*/
@Test
public void testStudentAdd() throws IOException {
String url="http://localhost:8090/studentAdd";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
PostMethod postMethod=new PostMethod(url);
postMethod.setRequestHeader("Content-Type", "application/json");
String requestBody="{\n" +
" \"className\": \"高一一班\",\n" +
" \"courseName\": \"英语\",\n" +
" \"email\": \"nuodashijie@163.com\",\n" +
" \"id\": 0,\n" +
" \"name\": \"六哥太快\",\n" +
" \"score\": 100,\n" +
" \"sex\": \"boy\",\n" +
" \"studentId\": \"66\"\n" +
"}";
postMethod.setRequestBody(requestBody);
try {
int status = httpClient.executeMethod(postMethod);
String responseBody = postMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responseBody:" + responseBody);
//将返回结果转换程json
JSONObject jsonObject=JSONObject.parseObject(responseBody);
//获取MSG中的内容
String data=jsonObject.getString("data");
jsonObject = JSONObject.parseObject(data);
String name=jsonObject.getString("name");
Assert.assertEquals(name,"六哥太快");
}
catch(Exception e){
throw new RuntimeException(e);
}
}
4、修改学生接口
/**
* 修改学生接口
*/
@Test
public void testStudentUpdate() throws IOException {
String url="http://localhost:8090/studentUpdate/176";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
PutMethod putMethod=new PutMethod(url);
//设置数组
NameValuePair[] nameValuePairs=new NameValuePair[7];
nameValuePairs[0]=new NameValuePair("studentId","66");
nameValuePairs[1]=new NameValuePair("name","六哥太快");
nameValuePairs[2]=new NameValuePair("sex","boy");
nameValuePairs[3]=new NameValuePair("className","高一一班");
nameValuePairs[4]=new NameValuePair("courseName","英语");
nameValuePairs[5]=new NameValuePair("score","100");
nameValuePairs[6]=new NameValuePair("email","nuodashijie@163.com");
putMethod.setRequestHeader("Content-Type", "application/json");
putMethod.setQueryString(nameValuePairs);
try {
int status = httpClient.executeMethod(putMethod);
String responseBody = putMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responseBody:" + responseBody);
//将返回结果转换程json
JSONObject jsonObject=JSONObject.parseObject(responseBody);
//获取MSG中的内容
String data=jsonObject.getString("data");
jsonObject = JSONObject.parseObject(data);
String studentId=jsonObject.getString("studentId");
Assert.assertEquals(studentId,"66");
}
catch(Exception e){
throw new RuntimeException(e);
}
}
5、查询学生接口
/**
* 查询学生接口 form格式
*/
@Test
public void testStudentFindById() throws IOException {
String url="http://localhost:8090/studentFindById";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
GetMethod getMethod=new GetMethod(url);
//设置数组
NameValuePair[] nameValuePairs=new NameValuePair[1];
nameValuePairs[0]=new NameValuePair("id","176");
getMethod.setRequestHeader("Content-Type", "application/json");
getMethod.setQueryString(nameValuePairs);
try {
int status = httpClient.executeMethod(getMethod);
String responseBody = getMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responseBody:" + responseBody);
//将返回结果转换程json
JSONObject jsonObject=JSONObject.parseObject(responseBody);
//获取MSG中的内容
String data=jsonObject.getString("data");
jsonObject = JSONObject.parseObject(data);
String email=jsonObject.getString("email");
Assert.assertEquals(email,"nuodashijie@163.com");
}
catch(Exception e){
throw new RuntimeException(e);
}
}
6、查询学生接口
/**
* 查询学生接口 Rest格式
*/
@Test
public void testStudentFindOne() throws IOException {
String url="http://localhost:8090/studentFindOne/176";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
GetMethod getMethod=new GetMethod(url);
getMethod.setRequestHeader("Content-Type", "application/json");
try {
int status = httpClient.executeMethod(getMethod);
String responseBody = getMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responseBody:" + responseBody);
//将返回结果转换程json
JSONObject jsonObject=JSONObject.parseObject(responseBody);
//获取MSG中的内容
String data=jsonObject.getString("data");
jsonObject = JSONObject.parseObject(data);
String score=jsonObject.getString("score");
Assert.assertEquals(score,"100");
}
catch(Exception e){
throw new RuntimeException(e);
}
}
7、删除学生接口
/**
* 删除学生接口
*/
@Test
public void testStudentDelete() throws IOException {
String url="http://localhost:8090/studentDelete/59";
//初始化客户端
HttpClient httpClient=new HttpClient();
//设置编码格式
httpClient.getParams().setContentCharset("utf-8");
//设置超时
httpClient.getParams().setConnectionManagerTimeout(5000);
httpClient.getParams().setSoTimeout(60*1000);
//选择请求方法
DeleteMethod deleteMethod=new DeleteMethod(url);
deleteMethod.setRequestHeader("Content-Type", "application/json");
try {
int status = httpClient.executeMethod(deleteMethod);
String responseBody = deleteMethod.getResponseBodyAsString();
System.out.println("status:" + status);
System.out.println("responseBody:" + responseBody);
//将返回结果转换程json
JSONObject jsonObject=JSONObject.parseObject(responseBody);
//获取MSG中的内容
String msg=jsonObject.getString("msg");
Assert.assertEquals(msg,"success");
}
catch(Exception e){
throw new RuntimeException(e);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律