【发布时间】:2017-01-14 05:29:01
【问题描述】:
对于 Spring MVC 测试(与 Java Hamcrest 一起使用):
测试需要渲染一个带有Model 对象的jsp 文件的场景,该对象只包含Person 类的一个实例,我有以下内容(工作正常):
.andExpect(model().size(1))
.andExpect(model().attributeExists("persona"))
.andExpect(model().errorCount(0))
.andExpect(model().hasNoErrors())
.andExpect(model().attribute("persona", notNullValue()))
.andExpect(model().attribute("persona", isA(Persona.class)))
.andExpect(model().attribute("persona", hasProperty("id", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("id", is(persona.getId()))))
.andExpect(model().attribute("persona", hasProperty("nombre", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("nombre", is(persona.getNombre()))))
.andExpect(model().attribute("persona", hasProperty("apellido", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("apellido", is(persona.getApellido()))))
.andExpect(model().attribute("persona", hasProperty("fecha", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("fecha", is(persona.getFecha()))));
我想知道是否可能以及如何计算 java 对象的属性/属性的数量,在本例中为 Person 类。
当Person 类有新字段时,我需要这个,即:age 和height。因此,测试方法应该无法让我更新count 号码并添加
.andExpect(model().attribute("persona", hasProperty("age", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("age", is(persona.getAge()))))
.andExpect(model().attribute("persona", hasProperty("height", notNullValue())))
.andExpect(model().attribute("persona", hasProperty("height", is(persona.getHeight()))));
json 中的类似
.andExpect(jsonPath("$").exists())
.andExpect(jsonPath("$.*", hasSize(is(4))))
【问题讨论】:
标签: spring spring-test hamcrest spring-test-mvc