Issue
I am testing endpoint with path para and query para and updating the request with some parameters. When I send the request content type is json and request looks good with the data that i am trying to change however when response is received
content type is text and i get 400 error bad request as status code and error message
javax.ws.rs.ProcessingException: RESTEASY008200: JSON Binding deserialization error: javax.json.bind.JsonbException: Can't infer a type for unmarshalling into: java.util.List
my code
@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());
}
Solution
As you have shared in your comment there, the endpoint is expecting a list of objects, not a singular object as you are sending... Just try wrapping that with a list, and you'd get past 400 error.
What you are sending;
{
"displayText": "Warrants",
"value": "true" // I don't know about this value field here
}
What is expected, as you've shared;
[
{
"displayText": "",
"preferences": [ { "category": "", "displaytext": "", } ],
"priority": "20"
}
]
One issue is that you have to send the object in a list, also passing objects as map is also somewhat counter productive, better to use the same object used in the RQ.
public class Request {
private String displayText;
private List<Preference> preferences;
private Integer priority;
//getter, setter,etc
}
& use it in your body in the rest assured test;
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);
Answered By - buræquete