简体   繁体   中英

Can't pass @Model parameter for junit controller test

I have a junit test for controller method, where I'm passing an @ModelAttribute object, with populated "name" field, like this

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));
    }

But when I check the controller method in debug, the field is empty

控制器发布方法

And I have no idea, why. Can someone explain or give some ideas what is wrong?

The problem was the wrong name of the attribute I am trying to pass. The @ModelAttribute name is "group" and the flashAttribute name is "groupDto".

I changed the name to "group" and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM