在 post api 上执行 mockMvc 时出错。 因为在这篇文章中,我的服务层会检查客户是否存在。 所以它给了我客户未找到的错误。 我怎么能在这里模拟客户?
final String PAYMENT_METHOD = "/customer/"+customerId+"/paymentMethod";
RequestBuilder request = MockMvcRequestBuilders.post(PAYMENT_METHOD).content(JsonUtils.toJson(paymentMethodDto))
.contentType(MediaType.APPLICATION_JSON)
.accept((MediaType.APPLICATION_JSON));
MvcResult result = mockMvc.perform(request).andExpect(status().isCreated()).andReturn();
Assert.assertEquals(result.getResponse().getStatus(), HttpStatus.CREATED.value());
ResponseEntity<PaymentMethodRes> responseEntity = paymentMethodController.addPaymentMethod(customerId, paymentMethodDto);
assertNotNull(responseEntity);
答案 0 :(得分:1)
应该可以
Mockito.when(customerService.findCustumerbyId(customerId).thenReturn(new Customer());`