使用 Spring 的 MockMvc 框架,如何测试 java.util.Map 模型属性的大小?
Using Spring's MockMvc framework, how do I test the size of a model attribute that is a java.util.Map?
我正在使用 Spring 3.2.11.RELEASE、JUnit 4.11 和 Mockito 1.9.5。使用 Spring 的 MockMvc 框架,如何测试模型中地图的大小?我正在尝试以下
mockMvc.perform(get("/admin/path/to/page")
.param(“param1”, param12))
.andExpect(status().isOk())
.andExpect(model().attribute("myMap", hasSize(1)))
但我收到了异常结果。
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.match(ModelResultMatchers.java:56)
at org.springframework.test.web.servlet.MockMvc.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
编辑:
我用那个方法有点麻烦。我已经导入了你提到的库并尝试像这样设置它......
mockMvc.perform(get(“/mypath”)
.param(“myparam”, param1))
.andExpect(status().isOk())
.andExpect(model().attribute(“myMapModelName”, org.hamcrest.collection.IsMapWithSize(3)))
但我在最后一行收到编译错误“找不到符号”。将此匹配器与 Spring 的 mockMVC 一起使用的正确方法是什么?
Hamcrest 2.0(Maven 坐标:org.hamcrest:java-hamcrest:2.0.0.0
)有一个 isMapWithSize
匹配器。如果你使用 Hamcrest 1.3,那么你必须自己创建这样一个匹配器。看看 Hamcrest 2.0 code.
我正在使用 Spring 3.2.11.RELEASE、JUnit 4.11 和 Mockito 1.9.5。使用 Spring 的 MockMvc 框架,如何测试模型中地图的大小?我正在尝试以下
mockMvc.perform(get("/admin/path/to/page")
.param(“param1”, param12))
.andExpect(status().isOk())
.andExpect(model().attribute("myMap", hasSize(1)))
但我收到了异常结果。
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.match(ModelResultMatchers.java:56)
at org.springframework.test.web.servlet.MockMvc.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
编辑:
我用那个方法有点麻烦。我已经导入了你提到的库并尝试像这样设置它......
mockMvc.perform(get(“/mypath”)
.param(“myparam”, param1))
.andExpect(status().isOk())
.andExpect(model().attribute(“myMapModelName”, org.hamcrest.collection.IsMapWithSize(3)))
但我在最后一行收到编译错误“找不到符号”。将此匹配器与 Spring 的 mockMVC 一起使用的正确方法是什么?
Hamcrest 2.0(Maven 坐标:org.hamcrest:java-hamcrest:2.0.0.0
)有一个 isMapWithSize
匹配器。如果你使用 Hamcrest 1.3,那么你必须自己创建这样一个匹配器。看看 Hamcrest 2.0 code.