Issue
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
mockMvc.perform(get("/admin/path/to/page")
.param(“param1”, param12))
.andExpect(status().isOk())
.andExpect(model().attribute("myMap", hasSize(1)))
but am getting the resulting exception.
java.lang.AssertionError: Model attribute 'myMap'
Expected: a collection with size <1>
but: was <{key1=org.mainco.subco.myproject.domain.MyprojectMyDto@724a133d}>
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)
Edit:
I’m having a little trouble with that method. I have imported the library you mentioned and tried to set it up like this …
mockMvc.perform(get(“/mypath”)
.param(“myparam”, param1))
.andExpect(status().isOk())
.andExpect(model().attribute(“myMapModelName”, org.hamcrest.collection.IsMapWithSize(3)))
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?
Solution
Hamcrest 2.0 (Maven coordinates: org.hamcrest:java-hamcrest:2.0.0.0
) has an isMapWithSize
matcher. If you use Hamcrest 1.3 than you have to create such a matcher by yourself. Have a look the Hamcrest 2.0 code.
Answered By - Stefan Birkner
Answer Checked By - Timothy Miller (JavaFixing Admin)