从 Json 路径查询返回并在 Junit 断言中使用的字符串的排序

Ordering of Strings returned from Json path query and used in a Junit Assertion

新手问题:

我必须针对页面上的地址进行断言。我首先查询 API 以获取地址,然后在网页上声明反对它。

这是我从 API 获取数据的方法:

   public Set<String> getPlaceAddressLinesFromApi(String addressId) {

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://app.ard.domain.local:8084/addresses/" + addressId);

    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        String json = content.toString();
        Set<String> addresslines = new HashSet<String>();
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.lines[0]").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.town").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.county").toString());
        addresslines.add(JsonPath.read(json, "$.location.postalAddress.postcode").toString());
        return addresslines;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

当我在断言中使用该方法时出现错误:

assertEquals(addressLinesFromApi, addressLinesFromWebsite, "The address displayed and address returned from api do not match");

Expected :TownHouse, Exeter, Devon, EX15 2LE
Actual   :[Exeter, Devon, EX15 2LE, TownHouse]

如何命令从 JSON 返回的字符串用于断言?

如何删除“[]”括号?

您可以将 addressLinesFromWebsite 拆分为 ,\s*HashSet 并进行比较:

assertEquals(addressLinesFromApi, new HashSet<String>(Arrays.asList(addressLinesFromWebsite.split(",\s*"))), "The address displayed and address returned from api do not match");