[英]Can't pass @Model parameter for junit controller test
我有一個控制器方法的 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));
}
但是當我在調試中檢查控制器方法時,該字段為空
我不知道,為什么。 有人可以解釋或給出一些想法有什么問題嗎?
問題是我試圖傳遞的屬性名稱錯誤。 @ModelAttribute 名稱為“group”,flashAttribute 名稱為“groupDto”。
我將名稱更改為“組”並且它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.