簡體   English   中英

Spring MVC:如何綁定到@ModelAttribute中的嵌套對象屬性

[英]Spring MVC: How to bind to nested object properties in the @ModelAttribute

我要根據以下代碼部分進行單元測試:

  @RequestMapping(value = "/changePass", method = RequestMethod.POST)
  public ModelAndView changePass(@ModelAttribute(TAPPLICATION) AppBean applicationBean, BindingResult result, ModelMap model, Principal principal, HttpServletRequest request) throws NSException, SQLException {
         // ...

         if (applicationBean != null
                    && applicationBean.getChangePassDto() != null
                    && StringUtils.isNotEmpty(applicationBean.getChangePassDto().getNewPassword())) {

                String newPassword = applicationBean.getChangePassDto().getNewPassword();
                // ...                   
            }
            // ...

AppBean包含以下getter和setter:

private ChangePassDto changePassDto;   

   public ChangePassDto getChangePassDto() {
        return changePassDto;
    }

    public void setChangePassDto(ChangePasswordDto changePassDto) {
        this.changePassDto = changePassDto;
    }

基本上,當我執行單元測試時,方法applicationBean.getChangePassDto()nullapplicationBean不為null。 如何初始化applicationBean.getChangePassDto()使其不返回null 我已經使用.param方法初始化了其他非對象參數,正如在單元測試中可以看到的那樣。

我還將Powermock用作單元測試框架。

請在我的單元測試中找到以下部分:

    @Before
    public void setup() {

        request = new MockHttpServletRequest();
        request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
        response = new MockHttpServletResponse();
        session = new MockHttpSession();
        request.setSession(session);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

        //Added viewResolver to prevent circular view path error
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");

        this.mockMvc = MockMvcBuilders.standaloneSetup(appController).setViewResolvers(viewResolver).build();    
    }

    @Test
    public void changePass_ExpectC() throws Exception {

        PowerMockito.doNothing().when(passwordVal).validate(any(User.class), anyListOf(Params.class), any(Object.class),any(Errors.class));


        mockMvc.perform(post("/changePass").param("userLogName", "JOHN").param("userLogged", "userLogged").param("password", "password123").param("newPassword", "newPassword123").param("confirmNewPassword", "newPassword123"))
                 .andExpect(view().name(Constants.DENIED))
                .andExpect(status().isOk()
                 );
    }

知道如何初始化applicationBean.getchangePassDto()使其不為null嗎?

在此先感謝您的幫助。

只需在您的AppBean創建一個ChangePassDto的新實例:

public class AppBean {

  private ChangePassDto changePassDto = new ChangePassDto();

  public ChangePassDto getChangePassDto() {
    return changePassDto;
  }

  public void setChangePassDto(ChangePasswordDto changePassDto) {
    this.changePassDto = changePassDto;
  }

  // ...
}

然后,您需要使用嵌套DTO中屬性的完整路徑,如下所示:

mockMvc.perform(post("/changePass")
    .param("changePassDto.userLogName", "JOHN")
    .param("changePassDto.userLogged", "userLogged")
    .param("changePassDto.password", "password123")
    .param("changePassDto.newPassword", "newPassword123")
    .param("changePassDto.confirmNewPassword", "newPassword123"))
.andExpect(view().name(Constants.DENIED))
.andExpect(status().isOk());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM