在下面的断言错误中需要帮助。 当我在下面进行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....
答案 0 :(得分:4)
capital
可能会返回多个值(值列表),因此您应该使用hasItem
Hamcrest匹配器:
RestAssured.get("http://restcountries.eu/rest/v1/name/Norway").
then().
body("capital",hasItem("Oslo"));
答案 1 :(得分:0)
您要提取的对象在列表/对象中,因此您不能在对象本身上使用equalTo。请使用hasItem匹配器。
body("capital",hasItem("Oslo"));