编程语言
首页 > 编程语言> > java-获取断言错误,即使期望值和实际值相同

java-获取断言错误,即使期望值和实际值相同

作者:互联网

在以下断言错误中需要帮助.
当我进行以下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"));

标签:rest-assured,java
来源: https://codeday.me/bug/20191027/1948415.html