如何从返回包含对象的对象的控制器测试SpringMVC模型属性
我有一个控制器,我正在尝试测试。我将myObj添加为属性,其中myObj本身作为一个对象如何从返回包含对象的对象的控制器测试SpringMVC模型属性,spring,spring-mvc,spring-boot,junit,mockmvc,Spring,Spring Mvc,Spring Boot,Junit,Mockmvc,我有一个控制器,我正在尝试测试。我将myObj添加为属性,其中myObj本身作为一个对象 public class MyObj { private List<OtherObj> otherObjList; private SecondObj secondObj; //getter setter } 这是我的测试用例。我正在尝试查看第一项中的属性 otherObjList是否有价值。这是我尝试过的,但不起作用 mockMvc.perform(get("/"))
public class MyObj {
private List<OtherObj> otherObjList;
private SecondObj secondObj;
//getter setter
}
这是我的测试用例。我正在尝试查看第一项中的属性
otherObjList是否有价值。这是我尝试过的,但不起作用
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(model().attribute("myObj.otherObjList", hasItems(hasProperty("id", is(12345)))));
当你想测试一个普通的控制器时,我认为你在正确的轨道上,但是。符号不合适。请查看以下示例测试断言:
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("todo/list"))
.andExpect(forwardedUrl("/WEB-INF/jsp/todo/list.jsp"))
.andExpect(model().attribute("todos", hasSize(2)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));
也许这个页面可以帮助您:我一直在为我的rest控制器使用jsonPath。这对控制器不起作用。请先看看我的问题。我返回的对象很复杂。
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("todo/list"))
.andExpect(forwardedUrl("/WEB-INF/jsp/todo/list.jsp"))
.andExpect(model().attribute("todos", hasSize(2)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute("todos", hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));