gpt4 book ai didi

java - Spring MVC : How to bind to nested object properties in the @ModelAttribute

转载 作者:行者123 更新时间:2023-12-02 03:38:20 25 4
gpt4 key购买 nike

我要根据以下代码部分进行单元测试:

  @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() 使其不为空吗?

提前感谢您的帮助。

最佳答案

只需在您的 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());

关于java - Spring MVC : How to bind to nested object properties in the @ModelAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37159631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com