2016-05-09 10 views
1

Im JUnit-Test unter ich die Validierung, dass ein Modell Attribut vorhanden ist, hat einen bestimmten Typ und ein bestimmtes Attribut:Wie kann ich testen, ob eine Eigenschaft eines Modellattributs bei der Verwendung von JUnit und MockMVC nicht null ist?

@Test 
public void myTest() throws Exception { 
    mockMvc.perform(get(MY_URL)) 
      .andExpect(status().is2xxSuccessful()) 
      .andExpect(view().name(MY_VIEW_NAME)) 
      .andExpect(model().attribute("myForm", instanceOf(MyForm.class))) 
      .andExpect(model().attribute("myForm", hasProperty("myProp"))); 
} 

Wie teste ich dann, dass diese Eigenschaft nicht null ist?

Antwort

1

Ich fand die Lösung. Sie können einfach org.hamcrest.beans.HasPropertyWithValue.hasProperty wie folgt verwenden:

hasProperty("myProp", notNullValue()))