/**
* 处理注册的提交信息
* @param spitter
* @return
*/
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegister(Spitter spitter){
spitterRepository.save(spitter);
//跳转到个人信息页面
return "redirect:/spitter/" + spitter.getUsername();
}
@Test
public void shouldProcessRegistration() throws Exception {
SpitterRepository mockRepository = mock(SpitterRepository.class);
Spitter unsaved = new Spitter("xu", "jiafei", "dale", "123456");
Spitter saved = new Spitter(10L,"xu", "jiafei", "dale", "123456");
//当调用保存方法时,返回指定的数据
when(mockRepository.save(unsaved)).thenReturn(saved);
SpitterController spitterController = new SpitterController(mockRepository);
MockMvc mockMvc = standaloneSetup(spitterController).build();
mockMvc.perform(post("/spitter/register")
.param("firstName","xu")
.param("lastName","jiafei")
.param("username","dale")
.param("password","123456"))
.andExpect(redirectedUrl("/spitter/dale"));
//此处若不重写Spitter的Equals方法和hashCode方法时将会报错,说两次的参数传递不一样
verify(mockRepository,atLeastOnce()).save(unsaved);
}
Argument(s) are different! Wanted:
spitterRepository.save(
Spitter{id=null, firstName='xu', lastName='jiafei', username='dale', password='123456'}
);
-> at test.SpitterControllerTest.shouldProcessRegistration(SpitterControllerTest.java:57)
Actual invocation has different arguments:
spitterRepository.save(
Spitter{id=null, firstName='xu', lastName='jiafei', username='dale', password='123456'}
);
-> at com.spittr.controller.SpitterController.processRegister(SpitterController.java:51)