作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在测试以下 Spring MVC Controller 方法 :
@RequestMapping(value = "/passwordReset", method = RequestMethod.POST, produces = "text/html")
public String resetPassword(@Validated({ ValidationGroups.PasswordReset.class }) @ModelAttribute PasswordInfo passwordInfo,
BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes, Locale locale) {
if (bindingResult.hasErrors()) {
model.addAttribute("passwordInfo", passwordInfo);
return "passwordReset";
}
redirectAttributes.addFlashAttribute("message", messageSource.getMessage("controller.preference.password_reset_ok", null, locale));
Member member = preferenceService.findMemberByToken(passwordInfo.getToken());
preferenceService.modifyPassword(member, passwordInfo.getNewPassword());
signinService.signin(member);
return "redirect:/preference/email";
}
@Test
public void resetPasswordShouldHaveNormalInteractions() throws Exception {
Member member = new Member();
when(preferenceService.findMemberByToken(eq("valid-token"))).thenReturn(member);
mockMvc.perform(post("/preference/passwordReset")//
.param("newPassword", "valid-password")//
.param("token", "valid-token"))//
.andDo(print())
.andExpect(redirectedUrl("/preference/email"))//
.andExpect(flash().attributeExists("message"))//FAILS HERE
.andExpect(flash().attributeCount(1));
verify(preferenceService).modifyPassword(eq(member), eq("valid-password"));
verify(signinService).signin(eq(member));
}
FAILS HERE
系统地未通过测试!
message
flash 属性确实在 FlashMap 中 (见
doPrint()
)如下:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /preference/passwordReset
Parameters = {newPassword=[valid-password], token=[valid-token]}
Headers = {}
Handler:
Type = com.bignibou.controller.preference.PreferenceController
Method = public java.lang.String com.bignibou.controller.preference.PreferenceController.resetPassword(com.bignibou.controller.preference.PasswordInfo,org.springframework.validation.BindingResult,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes,java.util.Locale)
Async:
Was async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = redirect:/preference/email
View = null
Model = null
FlashMap:
Attribute = message
value = null
MockHttpServletResponse:
Status = 302
Error message = null
Headers = {Location=[/preference/email]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = /preference/email
Cookies = []
最佳答案
如 doPrint() 的输出所示,FlashMap 包含一个值为 null 的属性“message”。
.andExpect(flash().attributeExists("message"))
attributeExists()
在
FlashAttributeResultMatcher
上但它只检查是否有
不为空 属性:
/**
* Assert the existence of the given flash attributes.
*/
public <T> ResultMatcher attributeExists(final String... names) {
return new ResultMatcher() {
public void match(MvcResult result) throws Exception {
for (String name : names) {
assertTrue("Flash attribute [" + name + "] does not exist", result.getFlashMap().get(name) != null);
}
}
};
}
关于由于某些奇怪的原因,Spring MVC 测试 3.2.2 失败了 "flash().attributeExists"断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051954/
本文整理了Java中bibliothek.util.xml.XElement.attributeExists()方法的一些代码示例,展示了XElement.attributeExists()的具体用法
我正在测试以下 Spring MVC Controller 方法 : @RequestMapping(value = "/passwordReset", method = RequestMethod.
我是一名优秀的程序员,十分优秀!