十三、postman导出java代码
- 导出成java的OkHttp代码
- 使用Junit进行接口自动化测试
- 使用fastJSON解析json字符串
创建个实体类
package com.netease.AcFunTest; public class V2exNode { private int id; private String name; private String url; private String title; private String title_alternative; private int topics; private int stars; private String headers; private String footer; private long created; private String avatar_mini; private String avatar_large; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getTitle_alternative() { return title_alternative; } public void setTitle_alternative(String title_alternative) { this.title_alternative = title_alternative; } public int getTopics() { return topics; } public void setTopics(int topics) { this.topics = topics; } public int getStars() { return stars; } public void setStars(int stars) { this.stars = stars; } public String getHeaders() { return headers; } public void setHeaders(String headers) { this.headers = headers; } public String getFooter() { return footer; } public void setFooter(String footer) { this.footer = footer; } public long getCreated() { return created; } public void setCreated(long created) { this.created = created; } public String getAvatar_mini() { return avatar_mini; } public void setAvatar_mini(String avatar_mini) { this.avatar_mini = avatar_mini; } public String getAvatar_large() { return avatar_large; } public void setAvatar_large(String avatar_large) { this.avatar_large = avatar_large; } }
引入3个jar包
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp --> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.7.5</version> </dependency>
执行测试类
package com.netease.AcFunTest; import com.alibaba.fastjson.JSON; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; import org.junit.*; import org.junit.jupiter.api.Test; public class V2exAPITest { @Test private void nodeApi() throws IOException{ OkHttpClient client = new OkHttpClient(); for (String nodeName : new String[]{"php","python","qna"}){ Request request = new Request.Builder(). url("https://www.v2ex.com/api/nodes/show.json?name="+nodeName).get().build(); Response response = client.newCall(request).execute(); V2exNode node = JSON.parseObject(response.body().string(),V2exNode.class); assertEquals(node.getName(),nodeName); } } }