我需要使用REST保证进行简单测试。这是指向Google Maps API的链接http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false
并且我需要验证该请求的statusCode是否正确(200)和字段:status:“确定”类型为“ street_address”国家/地区为“ US”
我写了这段代码,但是部分有效
public class RestTest {
@Test
public void Test1() {
RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
statusCode(200).
contentType(ContentType.JSON).
body("status", equalTo("OK")).
body("results.types", equalTo("street_address")).
body("results.address_components.short_name", equalTo("US"));
检查状态码,内容类型和正文的第一部分(状态= OK)工作正常,并通过了测试,但是我在最后两个正文测试中遇到问题,它们均失败了,我得到了:
JSON path results.types doesn't match.
Expected: street_address
Actual: [[street_address]]
在响应结果中是一个数组,而address_components也是一个数组。因此,在您的json路径中,您必须指定元素的索引。以下解决方案将起作用。
@Test
public void Test1() {
RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
statusCode(200).
contentType(ContentType.JSON).
body("status", equalTo("OK")).
body("results[0].types", contains("street_address")).
body("results[0].address_components[5].short_name", equalTo("US"));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句