需要以下场景的帮助:
我有下面的pojo类,当我使用restassured进行post调用时,我不想设置java类中的每个字段。要实现这些,我想维护一个createissue.json文件。在进行post调用时,我想从createissue.json文件中读取每个字段。
下面是我的pojo类createissuepayload.java
公共类createissuepayload{
@JsonProperty("summary")
private String summary;
@JsonProperty("description")
private String description;
@JsonProperty("issuetype")
private IssueType issuetype;
@JsonProperty("project")
private Project project;
public CreateIssuepayload(Project project, IssueType issuetype,String description, String summary) {
this.summary = summary;
this.description = description;
this.issuetype = issuetype;
this.project = project;
}
public CreateIssuepayload(Project project,IssueType issuetype,String description) {
this.description = description;
this.issuetype = issuetype;
this.project = project;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public IssueType getIssuetype() {
return issuetype;
}
public void setIssuetype(IssueType issuetype) {
this.issuetype = issuetype;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}
我的createissue.json文件
{
"fields":{
"summary":"Please look into issue",
"description":"Unable to create my JIRA ticket 3",
"issuetype":{
"name":"Bug"
},
"project":{
"key":"BP"
}
}
}
以及我的测试用例来发出post请求
@Test(enabled = false)
public static void test1() throws JsonProcessingException {
IssueType issuetype = new IssueType("**Bug**");
Project project = new Project("**BP**");
CreateIssuepayload mypojo = new CreateIssuepayload(project, issuetype, "**Unable to create my JIRA ticket 3**",
"**Please look into issue.....**");
Fields f = new Fields(mypojo);
RestAssured.baseURI = "http://localhost:8080";
Response res = given().header("Content-Type", "application/json")
.header("cookie", "JSESSIONID=" + Basic.sessionGen() + "").body(f).expect()
.body(containsString("greeting")).when().post("/rest/api/2/issue").then().extract().response();
}
在这里,我不想从java类中的testcase设置测试数据,比如bug、bp等,我想从json文件中动态读取它
注意:我也不想把整个json文件作为我的主体发布。
任何帮助都是值得的。谢谢你。
1条答案
按热度按时间v64noz0r1#
您可以使用java中的json简单库来读取json文件。maven存储库
然后以字符串形式检索值并创建
CreateIssuepayload
对象和Fields
物体。如果您按以下方式更改json文件,那么可以使用gson轻松地完成此操作
你不需要添加
@JsonProperty()
还有注解。然后使用
Gson
将json对象反序列化为java对象