4

当我尝试使用 MockServer 模拟外部 HTTP API 时,mockserver 返回java.lang.IllegalArgumentException

这是测试代码:

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

这是一个例外:

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

这是杰克逊的例外:

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
      }
   }

我正在尝试application/x-www-form-urlencoded使用正文发送请求

subject:integration-test-subject

.withQueryStringParameters(param("subject", "integration-test-subject"))测试中不存在时,它就可以了。

如何解决这个问题?

4

2 回答 2

2

这是 GitHub 中的一个问题,有解释

https://github.com/jamesdbloom/mockserver/issues/451

你可以更新到 5.4.1

于 2018-08-14T13:44:31.970 回答
1

此问题的解决方案是添加到您的项目中:

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

我的项目使用 Spring Boot 2 这个解决方案运行良好。

于 2018-04-02T10:49:38.233 回答