For Spring MVC Test (working together with Java Hamcrest):
Testing the scenario where is necessary render a jsp file with a Model
object which only contains an instance of the Person
class I have the following (works fine):
.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()))));
I want to know if is possible and how, count the number of attributes/properties of the java object, in this case Person
class.
I need this for the scenario when the Person
class has new fields, i.e: as age
and height
. Therefore the test method should fail to let me update the count
number and add
.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()))));
Something similar how in json
with
.andExpect(jsonPath("$").exists())
.andExpect(jsonPath("$.*", hasSize(is(4))))