Spring MVC testing: how to check if a model attribute does not exist?

Im using Spring 3.2.11.RELEASE. I am using the new MVC 3.2 mock framework for unit testing and usually I can check the attributes of the model using something like the following ...

private MockMvc mockMvc;
…

@Test
public final void test()
{
…
    mockMvc.perform(get("/mypath")
                    .param("userId", customerId))
                    .andExpect(status().isOk())
                    .andExpect(model().attribute("attr1", "value"))
                    .andExpect(view().name("my view"));

      

but how would I adjust the above to make sure the attr1 attribute was not included in my model?

+3


source to share


2 answers


Since the attributes of the model are stored in the map, you can check that the attribute value is null (assuming null is not a valid value for the attribute).

.andExpect(model().attribute("attr1", nullValue());

      



where nullValue()

- org.hamcrest.Matchers.nullValue

.

+4


source


An even lighter approach than Mark:



.andExpect(model().attributeDoesNotExist("attr"))

      

+5


source







All Articles