使用放心的put方法测试api时出现请求400错误和反序列化错误

时间:2019-06-28 00:34:51

标签: java rest rest-assured web-api-testing rest-assured-jsonpath

我正在使用路径参数和查询参数测试端点,并使用一些参数更新请求。当我发送请求时,内容类型为json 和请求看起来与我尝试更改的数据很好,但是当收到响应

内容类型为文本 我收到400错误的错误请求作为状态码 和错误消息

javax.ws.rs.ProcessingException:RESTEASY008200:JSON绑定反序列化错误:javax.json.bind.JsonbException:无法推断出要编组的类型:java.util.List

我的代码

@Given("^api is set up with required details$")
public void api_is_set_up_with_user_data() throws Throwable {
    loadProp();
    base_url = prop.getProperty("baseurl");
    RestAssured.baseURI = base_url;

}

@When("^a user retrieves the preferences by userid \"([^\"]*)\" and type \"([^\"]*)\"$")
public void a_user_retrieves_the_preferences_by_userid_and_type(String userid, String parameter) throws Throwable {
    request = given().pathParam("user_id", userid).queryParam("type", parameter);
    System.out.println(request);
    ENDPOINT_GET_USER_BY_ID = base_url + "{user_id}/preferences";
    response = request.when().get(ENDPOINT_GET_USER_BY_ID);
    System.out.println("response: " + response.prettyPrint());
}

@Then("^updates the value \"([^\"]*)\" of name \"([^\"]*)\"$")
public void updates_the_value_of_name(String value, String displaytext) throws Throwable {
    HashMap<String,String> post = new HashMap<String,String>();
    post.put("displaytext",displaytext);
    post.put("value",value);
    response = request.contentType("application/json").accept("*/*").body(post).put(ENDPOINT_GET_USER_BY_ID);
//        response = request.header("Content-Type", "application/json").body(post).put(ENDPOINT_GET_USER_BY_ID);

    System.out.println("Response : " + response.asString());
    System.out.println("Statuscode : " +response.getStatusCode());

}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

正如您在此处的评论中所共享的那样,端点期望的是对象列表,而不是发送时的单个对象...只要尝试用列表包装它,就会遇到400错误。 / p>

您要发送的内容;

{
    "displayText": "Warrants", 
    "value": "true"  // I don't know about this value field here
}

与您分享的期望一样;

[ 
    {
        "displayText": "", 
        "preferences": [ { "category": "", "displaytext": "", } ], 
        "priority": "20" 
    }
] 

一个问题是,您必须将对象发送到列表中,同时传递对象,因为map也有点适得其反,最好使用RQ中使用的相同对象。

public class Request {

    private String displayText;
    private List<Preference> preferences;
    private Integer priority;

    //getter, setter,etc
}

并在放心测试中在您的体内使用它;

List<Request> requestList = new ArrayList<>();
Request request = new Request();
request.setDisplayText("etc");
... // set other stuff
requestList.add(request);
response = request.contentType("application/json").accept("*/*").body(requestList).put(ENDPOINT_GET_USER_BY_ID);