Looks like your server is expecting a request body but you're sending the data as query parameters. If I understand it correctly you want to send your data as JSON. The easiest way to do this is using this approach:
Map<String, Object> map = new HashMap<>();
map.put("name", "Test12");
map.put("title", "Test127123");
map.put("contactEmail", "[email protected]");
map.put("description", "testing purpose");
ResponseBody body =
given().
accept(ContentType.JSON).
contentType(ContentType.JSON).
header("userid", "131987").
body(map).
when().
post("").
thenReturn().body();
Answer from Johan on Stack OverflowVideos
Looks like your server is expecting a request body but you're sending the data as query parameters. If I understand it correctly you want to send your data as JSON. The easiest way to do this is using this approach:
Map<String, Object> map = new HashMap<>();
map.put("name", "Test12");
map.put("title", "Test127123");
map.put("contactEmail", "[email protected]");
map.put("description", "testing purpose");
ResponseBody body =
given().
accept(ContentType.JSON).
contentType(ContentType.JSON).
header("userid", "131987").
body(map).
when().
post("").
thenReturn().body();
Check the fully qualified path for POST URI. The body for the post call is missing, which is mandatory. ur call should be like:
Response _res = requestspec.body(jsonObject).post(url);
Then you can perform operations on Response.
Restassured is failing to parse Json as per the stack trace. I use org.json jar, which is more elegant way to handle big json inputs. There are other implementations of json handling in java, which can be used based on your preference.
Coming to your code:
public void simpleTest() {
// use org.json JSONObject to define your json
JSONObject jsonObj = new JSONObject()
.put("phoneNumber","353837986524")
.put("messageContent","test");
given()
.port(31111) // port number
.contentType("application/json") //another way to specify content type
.body(jsonObj.toString()) // use jsonObj toString method
.when()
.post("/testenvironment/text/send")
.then()
.assertThat()
.body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
}
Also, I didnt find what the output of the rest service in the question. For example it is returning a a json {"resultMessage":"Message accepted"} you should be validating the response in the following way:
...
.body("resultMessage",equalTo("Message accepted"));
Try changing the mimeType to a header instead of a parameter.
And based on the information you shared I think what you need is the Content-Type header, not mimeType.