[英]How to test spring's @RequestBody which recieves custom object using MockMvc
我正在嘗試測試我的控制器方法,該方法將輸入參數作為應用程序中的對象之一。我知道如何使用String作為輸入參數,而不使用定制對象進行測試。
下面是我的應用程序中的代碼
@RequestMapping(value = "/someUrl", method = RequestMethod.POST)
public ResponseEntity<?> save(@RequestBody Transaction transaction) {
我以為下面的測試代碼是測試的方法之一,但是失敗了
@Test
public void test() throws Exception {
Transaction Transaction = new Transaction();
Gson gson = new Gson();
String json = gson.toJson(transaction);
mockMvc.perform(post("/someUrl")
.contentType(MediaType.APPLICATION_JSON)
.content(json));
當我運行上面的測試時,我收到錯誤消息:“ 實際上,該模擬的交互為零 ”。
您能幫我如何將自定義對象傳遞給MockMvc post方法。
mapper.writeValueAsString(json)
是一種更好的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.