【发布时间】:2019-01-04 10:01:22
【问题描述】:
我正在生成一些我正在使用 Wiremock 进行测试的 JSON。但是,我现在需要向 JSON 添加一个随机生成的字符串,该字符串在每次运行测试时都会发生变化。这会导致断言失败。有人对我如何解决这个问题有任何想法吗?
json:
"data": {
"case": {
"user": {
"name": {
"title": "Mr",
"firstName": "John",
"lastName": "Smith"
},
"address": { }
}
},
"subscriptions": {
"userSubscription": {
"randomNumber": "{{I am a random string so fail}}",
"email": null,
"mobile": null
},
"supporterSubscription" : null
}
}
测试:
@Test
public void should_handle_callback()
throws Exception {
submitStub();
HttpEntity<String> request = new HttpEntity<>("string");
// fails
ResponseEntity<CallbackResponse> result =
this.restTemplate.postForEntity(baseUrl, request, CallbackResponse.class);
}
private static String loadJson(String fileName) throws IOException {
URL url = getResource(fileName);
return Resources.toString(url, Charsets.toCharset("UTF-8"));
}
private void submitStub() throws Exception {
ccdServer.stubFor(post(concat(SUBMIT_EVENT_URL))
.withHeader(AUTHORIZATION, equalTo(USER_AUTH_TOKEN))
.withHeader(SERVICE_AUTHORIZATION_HEADER_KEY, equalTo(SERVICE_AUTH_TOKEN))
.withHeader(CONTENT_TYPE, equalTo(MediaType.APPLICATION_JSON_UTF8_VALUE))
.withRequestBody(equalToJson(createCaseRequest))
.willReturn(aResponse()
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withStatus(200)
.withBody(loadJson("testjson.json"))));
}
【问题讨论】:
-
randomNumber使用您的代码生成?你能模拟它,生成伪随机字符串吗?