title: Spring MVC: How to bind to nested object properties in the @ModelAttribute
body: <p>I have a unit test to carry out based on the following part of code:</p>
  
  <pre><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
                      &amp;&amp; applicationBean.getChangePassDto() != null
                      &amp;&amp; StringUtils.isNotEmpty(applicationBean.getChangePassDto().getNewPassword())) {
  
                  String newPassword = applicationBean.getChangePassDto().getNewPassword();
                  // ...                   
              }
              // ...
  </code></pre>
  
  <p>The AppBean contains the following getter and setter:</p>
  
  <pre><code>private ChangePassDto changePassDto;   
  
     public ChangePassDto getChangePassDto() {
          return changePassDto;
      }
  
      public void setChangePassDto(ChangePasswordDto changePassDto) {
          this.changePassDto = changePassDto;
      }
  </code></pre>
  
  <p>Basically when I execute the unit test the method <code>applicationBean.getChangePassDto()</code> is <code>null</code> but <code>applicationBean</code> is not null. How can I initialise the <code>applicationBean.getChangePassDto()</code> so that it does not return <code>null</code>? I have initialised the other non object parameters with the <code>.param</code> method as it can be seen in my unit test.</p>
  
  <p>I am also using Powermock as unit test framework.</p>
  
  <p>Please find below part of my unit test:</p>
  
  <pre><code>    @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()
                   );
      }
  </code></pre>
  
  <p>Any idea how I can intitialise <code>applicationBean.getchangePassDto()</code> so that it is not null?</p>
  
  <p>Thanks in advance for help.</p>