【发布时间】:2020-09-15 15:29:19
【问题描述】:
我对控制器方法进行了一个 junit 测试,其中我传递了一个 @ModelAttribute 对象,其中填充了“名称”字段,如下所示
public void when_GroupEditPostCreateEntityNotFoundException_Then_ErrorMessageCorrect() throws Exception {
GroupDTO groupDto = newGroupDto();
groupDto.setName("JustAnotherName");
int id = groupDto.getId();
when(groupService.create(groupDto)).thenThrow(EntityNotFoundException.class);
String msg = String.format(GROUP_EDIT_POST_ENFE_MSG, id);
this.mockMvc.perform(post("/groupEdit").flashAttr("groupDto", groupDto))
.andDo(print())
.andExpect(flash().attribute("errorMessage", msg));
}
但是当我在调试中检查控制器方法时,该字段为空
我也不知道,为什么。有人可以解释或给出一些想法有什么问题吗?
【问题讨论】:
-
是什么让您认为
flashAttr与@ModelAttribute相同?您不传递模型对象,而是传递绑定到GroupDTO的请求参数,就像在表单中一样。
标签: java spring model-view-controller junit