@Test
public void testWelcomePage() throws Exception {
UserDto dto = new UserDto("admin");
UserEntity user = new UserEntity("admin");
when(userServiceMock.getUser(dto)).thenReturn(user);
mockMvc.perform(get("/main/user/welcome?loginId=admin"))
.andExpect(status().isOk())
.andExpect(view().name("user/welcome"))
.andExpect(forwardedUrl("/WEB-INF/pages/user/welcome.jsp"))
.andExpect(model().attribute("user", hasProperty("loginId", is("admin")))); //-->java.lang.AssertionError: Model attribute 'user' .... but: was null...
verify(userServiceMock, times(1)).getUser(dto); //-->Argument(s) are different! Wanted:
verifyNoMoreInteractions(userServiceMock);
}
UserDto is object passed by spring mvc form object.
@RequestMapping(value="/welcome", method = RequestMethod.GET)
public String welcome(UserDto userDto, ModelMap model, Locale locale) {
UserEntity user = null;
try {
user = userService.getUser(userDto);
} catch (DataNotFoundException e) {
e.printStackTrace();
model.addAttribute("message", messageSource.getMessage("msg.data.notfound", null, locale));
}
model.addAttribute("user", user);
return "user/welcome";
}
But, mockito throws assertion error at passed argument(UserDto id different). How can I fix it?
UserDto object needs to override equals() method.
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
completed!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.