title: Using Spring's MockMvc framework, how do I test the size of a model attribute that is a java.util.Map?
body: <p>I’m using Spring 3.2.11.RELEASE, JUnit 4.11, and Mockito 1.9.5.  Using Spring’s MockMvc framework, how do I test the size of a map in my model?  I am trying the below </p>
  
  <pre><code>    mockMvc.perform(get("/admin/path/to/page") 
                      .param(“param1”, param12))
                      .andExpect(status().isOk())
                      .andExpect(model().attribute("myMap", hasSize(1)))
  </code></pre>
  
  <p>but am getting the resulting exception.</p>
  
  <pre><code>java.lang.AssertionError: Model attribute 'myMap'
  Expected: a collection with size &lt;1&gt;
       but: was &lt;{key1=org.mainco.subco.myproject.domain.MyprojectMyDto@724a133d}&gt;
      at org.springframework.test.util.MatcherAssertionErrors.assertThat(MatcherAssertionErrors.java:76)
      at org.springframework.test.web.servlet.result.ModelResultMatchers$1.match(ModelResultMatchers.java:56)
      at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:148)
      at org.mainco.subco.sbadmin.controllers.ContractsController2IT.testGetUpdateDistrictInfoLinkedTomyproject(ContractsController2IT.java:234)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
      at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:63)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>
  
  <p><b>Edit:</b></p>
  
  <p>I’m having a little trouble with that method.  I have imported the library you mentioned and tried to set it up like this …</p>
  
  <pre><code>    mockMvc.perform(get(“/mypath”) 
                      .param(“myparam”, param1))
                      .andExpect(status().isOk())
                      .andExpect(model().attribute(“myMapModelName”, org.hamcrest.collection.IsMapWithSize(3)))
  </code></pre>
  
  <p>but I’m getting a compilation error on the last line, “Cannot find symbol.”  What is the proper way to use this matcher with Spring’s mockMVC?                        </p>