2013-09-27 41 views
2

我测试了以下Spring MVC的控制器方法Spring MVC的测试3.2.2失败 “闪光()attributeExists。” 断言一些奇怪的原因

@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)); 
    } 

即使“消息”闪属性添加到重定向属性地图,Spring MVC的测试似乎并没有注意到它及以上行系统地通过测试失败!

你可以看到自己,message闪光属性确实是在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 = [] 

任何人都可以请帮助?仅供参考,我使用Spring 3.2.2。

回答

2

如doPrint()的输出所示,FlashMap包含一个属性“message”,其值为null。

.andExpect(flash().attributeExists("message")) 

呼吁的FlashAttributeResultMatcher但只检查attributeExists()如果有不为空属性:

/** 
    * 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); 
       } 
      } 
     }; 
    } 

因此断言失败。