在以下断言错误中需要帮助。
当我进行以下API调用时,尽管实际值和预期值都相同,但我却收到了此异常。
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.matcher.RestAssuredMatchers.*;
import org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.*;
public class firstRestCall
{
public static void main(String[] args)
{
RestAssured.get("http://restcountries.eu/rest/v1/name/Norway").
then().
body("capital",equalTo("Oslo"));
//body("capital",equalTo("[Oslo]")); tried this also, but getting the same exception
}
}
输出:
Exception in thread "main" java.lang.AssertionError: 1 expectation failed.
JSON path capital doesn't match.
Expected: Oslo
Actual: [Oslo]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) etc....
最佳答案
capital
可能返回多个值(值列表),因此您应该使用hasItem
Hamcrest匹配器:
RestAssured.get("http://restcountries.eu/rest/v1/name/Norway").
then().
body("capital",hasItem("Oslo"));