8

I am trying to create a test to validate the response of a JSON Post is as expected.

I am trying to test the POST of a JSON message body to a URL which is turn then sends a text message and if successful it sends a response that it was successful again in JSON format.

My test is as follows

public void simpleTest() {

      String myJson = "{\"phoneNumber\":\"353837986524\", \"messageContent\":\"test\"}";
      given()
              .port(31111) // port number
              .header("Content-Type", "application/json")
              .body(myJson)
              .when()
              .post("/testenvironment/text/send")
              .then().assertThat()
              .body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
  }

But seem to be getting this exception

java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!

And I'm not sure what the issue is?

1
  • 3
    Please don't change the intent of your question, as it invalidates answers. If you're having trouble implementing an answer, post a comment on it, and the answerer will receive a notification. If you end up with another question, then post it as a new question. You can link back here for context. Commented Jan 19, 2016 at 19:08

2 Answers 2

14

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I still get the same error groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'R', no possible valid JSON value or punctuation could be recognized. I will create a new question as my original one was different and it has edited
1

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.

5 Comments

This springs another exception groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'R', no possible valid JSON value or punctuation could be recognized.
Do you have a full stack trace print of the above exception or is above the only message you can see? Seems to be a JSON parsing error.
Just to confirm are you still using mimeType header or did you also change it to Content-Type as per my answer?
yes it's .header("Content-Type", "application/json") i'll update question
I think it might be better to create a separate question for the new exception maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.