Java Can';junit控制器测试的t pass@Model参数

Java Can';junit控制器测试的t pass@Model参数,java,spring,model-view-controller,junit,Java,Spring,Model View Controller,Junit,我有一个针对控制器的junit测试方法,其中我传递了一个@ModelAttribute对象,带有填充的“name”字段,如下所示 public void when_GroupEditPostCreateEntityNotFoundException_Then_ErrorMessageCorrect() throws Exception { GroupDTO groupDto = newGroupDto(); groupDto.setName("JustA

我有一个针对控制器的junit测试方法,其中我传递了一个@ModelAttribute对象,带有填充的“name”字段,如下所示

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));
    }
但是当我在debug中检查controller方法时,字段是空的


我不知道,为什么。有人能解释一下或给出一些想法吗?

问题是我试图传递的属性的名称错误。@modeldattribute名称为“group”,flashAttribute名称为“groupDto”


我将名称改为“group”,它起了作用。

是什么让你认为
flashAttr
@modeldattribute
相同?您不传递模型对象,而是传递绑定到
GroupDTO
的请求参数,就像在表单中一样。