1

我试图测试这个方法:Spring MVC的控制器测试

@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是一个枚举,该值将保存在数据库为整数,这是我的测试代码:

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

我得到这个Errror:

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

我试图通过这两个变量作为参数,只透过雇员为p arameter,但我仍然有相同的错误,除了参数参数必须都是String类型。 任何想法?

回答

1

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