【问题标题】:spring mvc controller Test with Enumeration value带有枚举值的spring mvc控制器测试
【发布时间】:2014-10-10 10:04:38
【问题描述】:

我正在尝试测试这个方法:

 @RequestMapping(value="/PersonalState/{EmployeeId}", method = RequestMethod.PUT)
public @ResponseBody Object Update(@PathVariable Integer EmployeeId, @RequestParam EmployeeState     empstate) throws Exception { 
    EmployeeService.updateEmployeeState(entityManager.find(Employee.class, EmployeeId), empstate);
    return null;
}

EmplyeeState 是一个枚举,值以整数形式保存在 db 中,这是我的测试代码:

@Test
public void EmployeeTest() throws Exception {   
        mockMvc.perform(MockMvcRequestBuilders.put("/PersonalState/{empstate}",EmplyeeState.PERMANENT)
           .param("EmployeeId", "550"))
          .andDo(print())
          .andExpect(MockMvcResultMatchers.status().isOk());
}   

我得到了这个错误:

             Resolved Exception:
             Type = org.springframework.beans.TypeMismatchException
             MockHttpServletResponse:
             Status = 400

我尝试将两个变量作为参数传递,仅将 EmployeeId 作为参数传递,但除了参数参数必须都是 String 类型之外,我仍然遇到相同的错误。 有什么想法吗?

【问题讨论】:

    标签: spring unit-testing model-view-controller controller enumeration


    【解决方案1】:

    问题已解决。 我将枚举字符串值作为参数传递。

    【讨论】:

    • 如果参数是枚举列表,你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2021-04-01
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多