【问题标题】:Can't pass @Model parameter for junit controller test无法通过 @Model 参数进行 junit 控制器测试
【发布时间】: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


【解决方案1】:

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

我将名称更改为“组”并且它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2014-06-26
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多