检查每个 Color 字段是否有一个颜色值(格式字符串“#FFFFFF”)
Check that each Color field has a color value (format string "#FFFFFF")
我有任务:“检查每个 Color 字段是否有一个颜色值(格式字符串“#FFFFFF”)”。
我有解决办法:
@Test
public void sixTest() {
Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
Response response = given()
.when()
.get("https://reqres.in/api/unknown")
.then()
.log().all()
.extract().response();
ResponseBody body = response.getBody();
String bodyAsString = body.asString();
Assert.assertEquals(bodyAsString.contains("#"), true, "Response body contains #");
}
我觉得我的问题解错了,请问有更合适的解法吗?
附加信息:URL https://reqres.in/api/unknown 是 public 和 returns 例如
{
page: 1,
per_page: 6,
total: 12,
total_pages: 2,
data: [
{
id: 1,
name: "cerulean",
year: 2000,
color: "#98B2D1",
pantone_value: "15-4020"
},
{
id: 2,
name: "fuchsia rose",
year: 2001,
color: "#C74375",
pantone_value: "17-2031"
},
...
这个问题可以用regular expressions
来解决
ResponseBody body = response.getBody();
String bodyAsString = body.asString();
//Regexp for a HEX color
String patternString = "#[a-zA-Z0-9]{6}"
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
//Check that the text matches the regular expression
boolean matches = matcher.matches();
您可以尝试使用正则表达式here
我和 Arthur 有相同的想法,但更具体要测试什么。
检查每种颜色是否匹配正则表达式 #[0-9A-Z]{6}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Response res = given().get("https://reqres.in/api/unknown");
List<String> colors = res.jsonPath().getList("data.color");
assertThat(colors, everyItem(matchesPattern("#[0-9A-Z]{6}")));
注意:Hamcrest 版本 1.x 没有方法 matchesPattern
将 2.2 版添加到您的 pom.xml
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
更多信息:api docs。
首先使用 json 解析器读取响应。
我用过杰克逊:
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.13.0
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.0
遍历到 data
数组节点并遍历每个元素并检查 color
节点后。
public static boolean everythingIsColor(String json) throws JsonProcessingException{
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonBody = objectMapper.readTree(json); //JsonProcessingException
Pattern pattern = Pattern.compile("#[0-9A-F]{6}"); //regex for color format
JsonNode dataNode = jsonBody.get("data");
for(JsonNode node : dataNode){
JsonNode colorNode = node.get("color");
if(!colorNode.isTextual()){ //is not text node
return false;
}
Matcher matcher = pattern.matcher(colorNode.asText());
if(!matcher.matches()){ //does not match with color format
return false;
}
}
//if reach here means every color node has a color value
return true;
}
我有任务:“检查每个 Color 字段是否有一个颜色值(格式字符串“#FFFFFF”)”。
我有解决办法:
@Test
public void sixTest() {
Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
Response response = given()
.when()
.get("https://reqres.in/api/unknown")
.then()
.log().all()
.extract().response();
ResponseBody body = response.getBody();
String bodyAsString = body.asString();
Assert.assertEquals(bodyAsString.contains("#"), true, "Response body contains #");
}
我觉得我的问题解错了,请问有更合适的解法吗?
附加信息:URL https://reqres.in/api/unknown 是 public 和 returns 例如
{
page: 1,
per_page: 6,
total: 12,
total_pages: 2,
data: [
{
id: 1,
name: "cerulean",
year: 2000,
color: "#98B2D1",
pantone_value: "15-4020"
},
{
id: 2,
name: "fuchsia rose",
year: 2001,
color: "#C74375",
pantone_value: "17-2031"
},
...
这个问题可以用regular expressions
来解决ResponseBody body = response.getBody();
String bodyAsString = body.asString();
//Regexp for a HEX color
String patternString = "#[a-zA-Z0-9]{6}"
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
//Check that the text matches the regular expression
boolean matches = matcher.matches();
您可以尝试使用正则表达式here
我和 Arthur 有相同的想法,但更具体要测试什么。
检查每种颜色是否匹配正则表达式 #[0-9A-Z]{6}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Response res = given().get("https://reqres.in/api/unknown");
List<String> colors = res.jsonPath().getList("data.color");
assertThat(colors, everyItem(matchesPattern("#[0-9A-Z]{6}")));
注意:Hamcrest 版本 1.x 没有方法 matchesPattern
将 2.2 版添加到您的 pom.xml
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
更多信息:api docs。
首先使用 json 解析器读取响应。 我用过杰克逊:
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.13.0 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.0
遍历到 data
数组节点并遍历每个元素并检查 color
节点后。
public static boolean everythingIsColor(String json) throws JsonProcessingException{
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonBody = objectMapper.readTree(json); //JsonProcessingException
Pattern pattern = Pattern.compile("#[0-9A-F]{6}"); //regex for color format
JsonNode dataNode = jsonBody.get("data");
for(JsonNode node : dataNode){
JsonNode colorNode = node.get("color");
if(!colorNode.isTextual()){ //is not text node
return false;
}
Matcher matcher = pattern.matcher(colorNode.asText());
if(!matcher.matches()){ //does not match with color format
return false;
}
}
//if reach here means every color node has a color value
return true;
}