如何设置 post 来自 json 文件的请求负载
How to set post request payload from json file restassured
在以下情况下需要帮助:
我有以下 pojo class,当我使用 restassured 进行 post 调用时,我不想在我的 java class.To 中设置每个字段来实现这些想要维护一个 createissue.json 个文件。
进行 post 调用时,我想从 createissue.json 文件中读取每个字段。
下面是我的pojoclassCreateIssuepayload.java
public class 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 中的测试用例中设置我的测试数据,如 Bug、BP 等 class.I 想从 json 文件
中动态读取它
注意:我也不想post整个json文件作为我的正文。
感谢任何帮助。
谢谢。
您可以使用json-Java中的简单库来读取json文件。 Maven repository
然后检索字符串形式的值并创建 CreateIssuepayload
对象和 Fields
对象。
@Test(enabled = false)
public static void test1() throws JsonProcessingException {
// Read the json file
org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("createissue.json"));
jsonObject = (org.json.simple.JSONObject) obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject fieldsObject = (JSONObject) jsonObject.get("fields");
JSONObject issueTypeObject = (JSONObject) fieldsObject.get("issuetype");
JSONObject projectObject = (JSONObject) fieldsObject.get("project");
IssueType issueType = new IssueType(issueTypeObject.get("name").toString());
Project project = new Project(projectObject.get("key").toString());
String summary = fieldsObject.get("summary").toString();
String description = fieldsObject.get("description").toString();
CreateIssuepayload mypojo = new CreateIssuepayload(project, issuetype, description, summary);
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();
}
如果您按如下方式更改 json 文件,您可以使用 Gson
轻松完成此操作
{
"summary": "Please look into issue",
"description": "Unable to create my JIRA ticket 3",
"issuetype": {
"name": "Bug"
},
"project": {
"key": "BP"
}
}
而且您也不需要添加 @JsonProperty()
注释。
然后使用Gson
将json对象反序列化为Java对象
@Test(enabled = false)
public static void test1() throws JsonProcessingException {
Gson gson = new Gson();
CreateIssuepayload mypojo = null;
try {
BufferedReader bf = new BufferedReader(new FileReader("createissue.json"));
mypojo = gson.fromJson(bf, CreateIssuepayload.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
RestAssured.baseURI = "http://localhost:8080";
Response res = given()
.header("Content-Type", "application/json")
.header("cookie", "JSESSIONID=" + Basic.sessionGen() + "")
.body(mypojo).expect()
.body(containsString("greeting")).when().post("/rest/api/2/issue").then().extract().response();
}
在以下情况下需要帮助:
我有以下 pojo class,当我使用 restassured 进行 post 调用时,我不想在我的 java class.To 中设置每个字段来实现这些想要维护一个 createissue.json 个文件。 进行 post 调用时,我想从 createissue.json 文件中读取每个字段。
下面是我的pojoclassCreateIssuepayload.java
public class 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 中的测试用例中设置我的测试数据,如 Bug、BP 等 class.I 想从 json 文件
中动态读取它注意:我也不想post整个json文件作为我的正文。
感谢任何帮助。 谢谢。
您可以使用json-Java中的简单库来读取json文件。 Maven repository
然后检索字符串形式的值并创建 CreateIssuepayload
对象和 Fields
对象。
@Test(enabled = false)
public static void test1() throws JsonProcessingException {
// Read the json file
org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("createissue.json"));
jsonObject = (org.json.simple.JSONObject) obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject fieldsObject = (JSONObject) jsonObject.get("fields");
JSONObject issueTypeObject = (JSONObject) fieldsObject.get("issuetype");
JSONObject projectObject = (JSONObject) fieldsObject.get("project");
IssueType issueType = new IssueType(issueTypeObject.get("name").toString());
Project project = new Project(projectObject.get("key").toString());
String summary = fieldsObject.get("summary").toString();
String description = fieldsObject.get("description").toString();
CreateIssuepayload mypojo = new CreateIssuepayload(project, issuetype, description, summary);
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();
}
如果您按如下方式更改 json 文件,您可以使用 Gson
轻松完成此操作{
"summary": "Please look into issue",
"description": "Unable to create my JIRA ticket 3",
"issuetype": {
"name": "Bug"
},
"project": {
"key": "BP"
}
}
而且您也不需要添加 @JsonProperty()
注释。
然后使用Gson
将json对象反序列化为Java对象
@Test(enabled = false)
public static void test1() throws JsonProcessingException {
Gson gson = new Gson();
CreateIssuepayload mypojo = null;
try {
BufferedReader bf = new BufferedReader(new FileReader("createissue.json"));
mypojo = gson.fromJson(bf, CreateIssuepayload.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
RestAssured.baseURI = "http://localhost:8080";
Response res = given()
.header("Content-Type", "application/json")
.header("cookie", "JSESSIONID=" + Basic.sessionGen() + "")
.body(mypojo).expect()
.body(containsString("greeting")).when().post("/rest/api/2/issue").then().extract().response();
}