简体   繁体   English

执行 MockMvc:在 api 后执行 mockMvc 时出错

[英]performing MockMvc : getting error while performing mockMvc on post api

getting error while performing mockMvc on post api.在 api 后执行 mockMvc 时出错。 because in this post call my service layer checks the customer is exist or not.因为在这篇文章中调用我的服务层检查客户是否存在。 so it is giving me customer not found error.所以它给了我客户未找到的错误。 how can i mock customer here?我怎么能在这里模拟客户?

        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);


That should work那应该工作

Mockito.when(customerService.findCustumerbyId(customerId).thenReturn(new Customer());`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM