使用Spring的MockMvc框架,如何测试作为java.util.Map的模型属性的大小?

时间:2016-01-19 19:51:56

标签: spring dictionary junit hamcrest mockmvc

我正在使用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$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)

修改

我对这种方法有点麻烦。我已经导入了你提到的库并试图像这样设置它......

    mockMvc.perform(get(“/mypath”) 
                    .param(“myparam”, param1))
                    .andExpect(status().isOk())
                    .andExpect(model().attribute(“myMapModelName”, org.hamcrest.collection.IsMapWithSize(3)))

但是我在最后一行收到了编译错误,“无法找到符号。”在Spring的mockMVC中使用此匹配器的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

Hamcrest 2.0(Maven坐标:.cs file)有一个org.hamcrest:java-hamcrest:2.0.0.0匹配器。如果您使用Hamcrest 1.3,则必须自己创建这样的匹配器。看看Hamcrest 2.0 code