[英]While unit testing REST endpoint, LocalDateTime is sent truncated
我想對REST端點進行單元測試。 我對此很陌生,但是我能夠編寫以下代碼:
HttpPost request = new HttpPost("http://localhost:8080/myrest");
ObjectMapper mapper = new ObjectMapper();
String empJson = mapper.writeValueAsString(clientDto);
StringEntity input = new StringEntity(empJson);
input.setContentType("application/json");
request.setEntity(input);
org.apache.http.HttpResponse httpResponse =
HttpClientBuilder.create().build().execute(request);
clientDto
是具有字段LocalDateTime
類型的簡單POJO的實例。 事實是,我的反序列化程序始終以以下字符串{
(僅一個字符)的形式獲取該日期。 我該如何解決? 如果您認為可以以更好的方式完成單元測試,請告訴我。 我已經使用過Apache依賴性,但這不是必須的。
使用RestAssured
庫,測試REST API非常容易。 請看一下這段代碼。 它的優點之一是它是不言自明的。
CreditClass dto = new CreditClass();
dto.setCreditGranted(false);
given()
.contentType(MediaType.APPLICATION_JSON)
.body(dto)
.when().post("localhost:8080/rest-api").then()
.statusCode(200)
.body("creditGranted", equalTo(true));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.