0

This is my method for POST test

public void createParagraph3() {
    RestAssured.baseURI = paragraphsURL;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("featurePackage", Arrays.asList(new HashMap<String, String>() {{
            put("name", "Test");
            put("inputText", "test test");
    }}));
    map.put("features", Arrays.asList(new HashMap<String, Object>() {{
            put("featureType", "Feature");
            put("inUse", "true");
            put("name", "test xyxy");
    }}));
    map.put("type", Arrays.asList(new HashMap<String, String>() {{
        put("key", "int");
        put("name", "Introduction");
    }}));
    RequestSpecification request = RestAssured.given();
    request.header("Content-Type", "application/json");
    request.body(map).toString();
    Response response = request.post();
    int statusCode = response.getStatusCode();
    System.out.println("Response body: " + response.body().asString());
    System.out.println("Status code received: " + statusCode);
}

Below I have my request on basis I'm creating my test

{
  "featurePackage": {
    "features": [
      {
        "featureType": "string",
        "id": 0,
        "inUse": true,
        "name": "string"
      }
    ],
    "id": 0,
    "name": "string",
    "objectCount": 0
  },
  "features": [
    {
      "featureType": "string",
      "id": 0,
      "inUse": true,
      "name": "string"
    }
  ],
  "id": 0,
  "inputText": "string",
  "objectCount": 0,
  "outputHtmlText": "string",
  "sourceFileName": "string",
  "type": {
    "key": "string",
    "name": "string"
  }
}

What I am doing wrong? I still received 400 response "Cannot deserialize instance". Can someone help? I have incomplete request in my method?

goney225
  • 91
  • 1
  • 1
  • 10

1 Answers1

0

EDIT 1:

The root cause is that map.toString() is not a valid JSON string.

There are two ways for you to solve this problem:

  1. use JSONObject instead of map, see this
  2. convert map to JSON object, see this

PS: A demo about map.toString()

Map<String, Integer> map = new HashMap<>();
map.put("apple",  1);
map.put("banana", 2);


System.out.println(map.toString());
// get: {apple=1, banana=2} is NOT a valid JSON String
// but, {"apple":1,"banana":2} is a valid JSON String

EDIT 2:

Since the owner of question added some comments, I have to add the following answer.

Pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.10.0</version>
</dependency>

Java code:

// your comment
Map<String, Object> titles = new HashMap<>();
titles.put("titles", Arrays.asList(new HashMap<String, Object>() {
    {
        put("id", "876");
        put("title", "test");
    }
}));
System.out.println(titles);

// my new answer
import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(titles);
System.out.println(json);
Peter Quan
  • 788
  • 4
  • 9
  • OK I used JSONObject and I have following result "bad request 400 - required request body is missing – goney225 Aug 28 '20 at 07:24
  • @goney225 I think "bad request 400 - required request body is missing" is caused by not passing body or body being null. You can debug and watch what the request body is, or just talk with the developer who has this API to ask for help. – Peter Quan Aug 28 '20 at 08:20
  • @goney225, if this is the solution to your problem, please [Accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) or [Vote up it](https://stackoverflow.com/help/privileges/vote-up) :) – Peter Quan Sep 09 '20 at 03:23
  • Currenty I have below error Bad Request","message":"JSON parse error: Unexpected character ('t' (code 116)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): – goney225 Sep 30 '20 at 11:16
  • Sounds like another question?? I think the error message is clear, and you can look for answers from the following aspects: 1)JSON parse error 2)character 't' needs double-quote to start field name – Peter Quan Sep 30 '20 at 13:47
  • For example Map titles = new HashMap<>(); titles.put("titles", Arrays.asList(new HashMap() {{ put("id", "876"); put("title", "test"); }})); I have double-quote and I don't understand why I received this error – goney225 Oct 01 '20 at 09:30
  • @goney225 I have modified the answer, please check it again. Actually, the original answer has told you the truth, "convert map to JSON object, do not use map directly!" – Peter Quan Oct 10 '20 at 01:45