简体   繁体   English

Spring MVC:如何绑定到@ModelAttribute中的嵌套对象属性

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

I have a unit test to carry out based on the following part of code: 我要根据以下代码部分进行单元测试:

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

The AppBean contains the following getter and setter: AppBean包含以下getter和setter:

private ChangePassDto changePassDto;   

   public ChangePassDto getChangePassDto() {
        return changePassDto;
    }

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

Basically when I execute the unit test the method applicationBean.getChangePassDto() is null but applicationBean is not null. 基本上,当我执行单元测试时,方法applicationBean.getChangePassDto()nullapplicationBean不为null。 How can I initialise the applicationBean.getChangePassDto() so that it does not return null ? 如何初始化applicationBean.getChangePassDto()使其不返回null I have initialised the other non object parameters with the .param method as it can be seen in my unit test. 我已经使用.param方法初始化了其他非对象参数,正如在单元测试中可以看到的那样。

I am also using Powermock as unit test framework. 我还将Powermock用作单元测试框架。

Please find below part of my unit test: 请在我的单元测试中找到以下部分:

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

Any idea how I can intitialise applicationBean.getchangePassDto() so that it is not null? 知道如何初始化applicationBean.getchangePassDto()使其不为null吗?

Thanks in advance for help. 在此先感谢您的帮助。

Simply create a new instance of ChangePassDto in your AppBean : 只需在您的AppBean创建一个ChangePassDto的新实例:

public class AppBean {

  private ChangePassDto changePassDto = new ChangePassDto();

  public ChangePassDto getChangePassDto() {
    return changePassDto;
  }

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

  // ...
}

You then need to use the full path to the properties in the nested DTO like this: 然后,您需要使用嵌套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