在 Restcontroller 的单元测试期间,我的 Mocking class 不起作用

During unit test of Restcontroller my Mocking class does not work

我需要使用 mock class 测试 RestController 中的一种方法。但是 java 不明白,然后模拟 class 并尝试调用它。但是,这种具有相同模拟 class 的方法成功了。 我的休息控制器:

@RestController
public class OrderController {

    @Autowired
    ServiceOrder serviceOrder;

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/orders", method= RequestMethod.POST, produces={"application/json; charset=UTF-8"})
    public List<Order> sortOrders(@RequestParam("field") String field) {
        return serviceOrder.sortOrders(field);

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/orders/completed/period", method= RequestMethod.POST, produces={"application/json"})
    public long showCompletedOrdersInPer(
            @RequestParam (value = "start") String startDate,
            @RequestParam (value = "end") String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = LocalDate.parse(startDate, formatter);
        LocalDate end = LocalDate.parse(endDate, formatter);
        return serviceOrder.completedOrdersInPeriod(start, end);
    }

好吧,方法 sortOrders() 的测试没问题,但是 showCompletedOrdersInPer() 的测试失败了。 我的测试 class:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ControllersTestConfig.class})
@WebAppConfiguration
public class OrderControllerTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    OrderController orderController;

    @Autowired
    ServiceOrder serviceOrder;

    private MockMvc mockMvc;
    @BeforeEach
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
    }

    // Test from method OrderController.sortOrders
    // Description: we should get response.status OK
    @Test
    public void sortOrdersTest() throws Exception {
        Mockito.when(serviceOrder.sortOrders(any())).thenReturn(null);
        mockMvc.perform(MockMvcRequestBuilders.post("/orders?field=1")).andExpect(MockMvcResultMatchers.status().isOk());
    }

    // Test from method OrderController.showCompletedOrdersInPer
    // Description: we should get response.status OK
    @Test
    public void showCompletedOrdersInPerTest() throws Exception {
        Mockito.when(serviceOrder.completedOrdersInPeriod(any(), any())).thenReturn(1L);
        mockMvc.perform(MockMvcRequestBuilders.post("/orders/completed/period?start=2020-01-01&end=2022-01-01")).andExpect(MockMvcResultMatchers.status().isOk());
    }

这是配置类:

public class ControllersTestConfig {

    @Bean
    public ServiceOrder serviceOrder() {
        return Mockito.mock(ServiceOrder.class);
    }
    
    @Bean
    public OrderController orderController(){
        return new OrderController();
    }

}

当我运行 sortOrdersTest()时,测试正常,当我运行 showCompletedOrdersInPerTest()时,我有

Status expected:<200> but was:<500>
Expected :200
Actual   :500

如果我 运行 使用调试进行测试,我明白了,然后在 sortOrders() 模拟工作并且 serviceOrder.sortOrders(field) 不调用,并且在 showCompletedOrdersInPer() 模拟不工作并且 java 尝试调用 serviceOrder.completedOrdersInPeriod(start, end),我的状态为 500。 请帮帮我!

恩,是在dao层把Integer解析成Long的问题。我解决了它并测试完成。