Using the Spring MockMvc structure, how can I check the value of the attribute attribute of my model?

I am using Spring 3.2.11.RELEASE and JUnit 4.11. I am using the Springs org.springframework.test.web.servlet.MockMvcFramework to test the controller method. In one test, I have a model that is populated with the following object:

public class MyObjectForm 
{

    private List<MyObject> myobjects;

    public List<MyObject> getMyObjects() {
        return myobjects;
    }

    public void setMyObjects(List<MyObject> myobjects) {
        this.myobjects = myobjects;
    }

}

The object "MyObject", in turn, has the following field ...

public class MyObject
{
    private Boolean myProperty;

Using the MockMvc framework, how can I verify that the first item in the "myobjects" list has the attribute "myProperty" equal to true? As long as I know this is happening like this ...

    mockMvc.perform(get("/my-path/get-page")
            .param("param1", ids))
            .andExpect(status().isOk())
            .andExpect(model().attribute("MyObjectForm", hasProperty("myobjects[0].myProperty", Matchers.equalTo(true))))
            .andExpect(view().name("assessment/upload"));

but i don't know how to check attribute attribute value?

+5
source share
2 answers

hasItem hasProperty, getter getMyProperty.

.andExpect(model().attribute("MyObjectForm",
   hasProperty("myObjects",
       hasItem(hasProperty("myProperty", Matchers.equalTo(true))))))

, ,

.andExpect(model().attribute("MyObjectForm",
   hasProperty("myObjects", contains(
         hasProperty("myProperty", Matchers.equalTo(true)),
         any(MyObject.class),
         ...
         any(MyObject.class)))));
+10

, - . , (firstName) (Customer) List < > . :

.andExpect(model().attribute("customerList", Matchers.hasItemInArray(Matchers.<Customer> hasProperty("firstName", Matchers.equalToIgnoringCase("Jean-Luc")))))
0

Source: https://habr.com/ru/post/1624820/


All Articles