我在Java + Maven + Cucumber上进行了测试。 测试会创建对远程API(https://jsonplaceholder.typicode.com/)的请求,并检查json中收到的答案是否正确。
黄瓜情况:
Scenario Outline: Check a post body
When user requests for the post by it's <id> as id
Then response code is 200
And response for the post returns correct body <body>
Examples:
|id |body|
|1 |quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto|
|18 |eveniet quo quis\nlaborum totam consequatur non dolor\nut et est repudiandae\nest voluptatem vel debitis et magnam|
... etc...
Json响应由ObjectMapper解析并存储在Java类中:
package responses;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Posts {
public Posts(){};
private int userId;
private int id;
private String title;
private String body;
public int getUserId() {return userId;}
public void setUserId(int userId) {this.userId = userId;}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public String getBody() {return body;}
public void setBody (String body) {this.body = body;}}
。
BufferedReader br = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
ObjectMapper mapper = new ObjectMapper();
Posts post = mapper.readValue(br, Posts.class);
然后比较两个字符串:
@Then("^response for the post returns correct body (.*)")
public void response_for_the_post_returns_correct_body(String body) throws IOException {
Assert.assertEquals(body, RequestSender.get_data_from_post("body", connection));
}
使用上述设置,当我运行单个方案时,在Idea中将其标记为“通过”。但是当我使用maven'mvn test'运行所有测试时,测试失败。
但是,如果我将“主体字符串”更改为与“ 12345”完全不同的内容,那么当我运行单个方案时,它也会失败。
问题仅在于此“ Body”参数(我想是因为它包含多字符串值)
为什么会发生?我该如何解决?