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

Bad Request 400 and deserialization error while testing api using rest assured put method

我正在使用路径参数和查询参数测试端点并使用一些参数更新请求。当我发送请求内容类型是 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());

}

正如您在那里的评论中所分享的那样,端点需要一个 object 的列表,而不是您发送的单个 object ......只需尝试用列表包装它,你会得到超过 400 错误。

您要发送的内容;

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

正如您所分享的那样,预期是什么;

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

一个问题是你必须在列表中发送 object,同时传递 object 作为 map 也会适得其反,最好使用相同的 object在 RQ 中。

public class Request {

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

    //getter, setter,etc
}

&在你的body放心测试中使用它;

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);