Search code examples
jacksonmockserver

MockServer: IllegalArgumentException when parsing JSON


When i'm trying to mock external HTTP API with MockServer, mockserver returns java.lang.IllegalArgumentException

This is the test code:

new MockServerClient("localhost", 1080)
    .when(request("/messages")
    .withMethod("POST")
    .withQueryStringParameters(
        param("subject", "integration-test-subject")
    )
).respond(response().withStatusCode(200));

This is the exception:

java.lang.IllegalArgumentException: Exception while parsing 
[  
   {  
      "httpRequest":{  
         "method":"POST",
         "path":"/messages",
         "queryStringParameters":{  
            "subject":[  
               "integration-test-subject"
            ]
         }
      },
      "httpResponse":{  
         "statusCode":200
      },
      "times":{  
         "remainingTimes":0,
         "unlimited":true
      },
      "timeToLive":{  
         "unlimited":true
      }
   }
] for Expectation

And this is the Jackson exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of FIELD_NAME token
at
[  
   Source:(String)"   {  
      "httpRequest":{  
         "method":"POST",
         "path":"/messages",
         "queryStringParameters":{  
            "subject":[  
               "integration-test-subject"
            ]
         }
      },
      "httpResponse":{  
         "statusCode":200
      },
      "times":{  
         "remainingTimes":0,
         "unlimited":true
      },
      "timeToLive":{  
         "unlimited":true
      }
   }

I'm trying to send application/x-www-form-urlencoded request with body

subject:integration-test-subject

When .withQueryStringParameters(param("subject", "integration-test-subject")) is not present in test, then it goes OK.

How to fix this?


Solution

  • the solution of this problem is to add to your project:

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

    im my project with Spring Boot 2 this solution worked fine.