Testing for a null model attribute in Spring

I have been using Mockito to test my Spring controller methods but am confused with the behaviour of the following failing test:

Controller:

@RequestMapping(value = "/getPage", method = RequestMethod.GET)
public String getPage(Model model) {

    String myString = myService.someMethod();

    model.addAttribute("myString", myString);
    return "myTemplate";
}

Test:

@Test public void testGetPage() throws Exception {

    String myString = null;

    when(mockService.someMethod()).thenReturn(myString);

    mockMvc.perform(get("/getPage"))
        .andExpect(status().isOk())
        .andExpect(model().size(1)) // this passes
        .andExpect(model().attribute("myString", myString))   // fails: "Model attribute 'myString' does not exist 
        .andExpect(model().attributeExists("myString"))    // fails: "Model attribute 'myString' does not exist 
        ;
 }

So what is the test detecting when it returns model().size(1) if not the myString variable? Is there a way I can list all the attributes associated with the model?

Answers


The model is a map with a key of myString and value of null. To make the model size zero, don't add any attributes to it.

You could have 1000 attributes, all initialized to null. Size would be 1000.


Need Your Help

working with php and python together

php python mysql excel

currently i am working on a project which is related to automation.

Conversion from string to type 'Double' is not valid

ms-access oledbcommand

I am getting the following error while running a select statement (using OleDbCommand).