简体   繁体   English

我应该为此特定方法创建2个单元测试吗? Spring MVC应用

[英]Should I create 2 unit tests for this specific method? Spring MVC app

Here's the method code: 这是方法代码:

@RequestMapping("/employee/{id}")
public String showSpecificEmployee(@PathVariable String id, 
@RequestParam(name = "date", required = false) String date, Model model){

    if(date == null)
        date = YearMonth.now().toString();

    model.addAttribute("employee", employeeService.findEmployeeWithFilteredWorkdaysAndPayments(new Long(id), date));

    return "specificEmployee";
}

So far I've created a test: 到目前为止,我已经创建了一个测试:

@Test
public void showSpecificEmployee() throws Exception {

    //given
    Employee employee = new Employee();

    //when
    when(employeeService.findEmployeeWithFilteredWorkdaysAndPayments(anyLong(), anyString())).thenReturn(employee);

    //then
    mockMvc.perform(get("/employee/1"))
            .andExpect(status().isOk())
            .andExpect(view().name("specificEmployee"))
            .andExpect(model().attributeExists("employee"));

    verify(employeeService, times(1)).findEmployeeWithFilteredWorkdaysAndPayments(anyLong(), anyString());
}

Is this fine? 这样好吗 Or should I write another one to check whether the date is created properly? 还是我应该再写一个检查date是否正确创建? Any other advices much appreciated. 任何其他建议,不胜感激。

Yes, you should create 2 test case. 是的,您应该创建2个测试用例。 One case is for not null date, and another case is for null date. 一种情况是非空日期,另一种情况是空日期。
If you check your test coverage with jacoco or something, you can see what's different between two cases visually. 如果使用jacoco或其他工具检查测试范围,则可以直观地看到两种情况之间的区别。

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

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