关于Spring MVC测试API的模型()。attribute()方法的询问

时间:2013-09-20 15:02:22

标签: spring spring-mvc mockito hamcrest spring-mvc-test

我正在尝试使用 Spring MVC测试API 测试以下控制器方法:

@RequestMapping(value = "/preference/email", method = RequestMethod.GET, produces = "text/html")
public String emailForm(@ModelAttribute EmailInfo emailInfo, Model model, @CurrentMember Member member) {
    emailInfo.setEmail(member.getEmail());
    emailInfo.setActivated(member.isActivated());
    emailInfo.setToken(member.getToken());
    model.addAttribute("emailInfo", emailInfo);
    return "preference";
}

当我调试以下测试方法时......

@Test
    public void shouldPopulateEmailInfo() throws Exception {
        when(currentMemberHandlerMethodArgumentResolver.supportsParameter(any(MethodParameter.class))).thenReturn(Boolean.TRUE);
        when(currentMemberHandlerMethodArgumentResolver.resolveArgument(any(MethodParameter.class), any(ModelAndViewContainer.class), any(NativeWebRequest.class), any(WebDataBinderFactory.class))).thenReturn(currentMember);
        mockMvc.perform(get("/preference/email"))//
                .andDo(print())//
                .andExpect(model().attribute("emailInfo.email", "currentMember@example.com"));//
    }

...我确实在emailInfo 的电子邮件字段中设置了“ currentMember@example.com ”。

然而,我系统地得到:

java.lang.AssertionError: Model attribute 'emailInfo.email' expected:<currentMember@example.com> but was:<null>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
    at org.springframework.test.web.servlet.result.ModelResultMatchers$2.match(ModelResultMatchers.java:68)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
    at com.bignibou.tests.controller.preference.PreferenceControllerEmailManagementTest.shouldPopulateEmailInfo(PreferenceControllerEmailManagementTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    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.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:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    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:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我还没有使用Spring Mvc Test API。有人可以帮忙吗?

修改:以下是我在考虑提供的答案后提出的问题:

.andExpect(model().attribute("emailInfo", Matchers.hasProperty("email", Matchers.equalTo("currentMember@example.com"))));//

现在工作正常。

2 个答案:

答案 0 :(得分:15)

正如Sotirios指出的那样,model().attribute("attributeName")不允许你导航,它会将整个事物用作名称。

但是,您可以使用此方法:

public <T> ResultMatcher attribute(final String name, final Matcher<T> matcher)

这将允许您指定Hamcrest匹配器以再次匹配您的属性,如:

.andExpect(model().attribute("emailInfo", hasProperty("email", is("currentMember@example.com"))))

确保在类路径中有Hamcrest库并将其导入为:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*

注意:如果您使用Groovy进行测试(Spock),导入是特别重要的,因为Hamcrest中的方法名称与Groovy标准库冲突

答案 1 :(得分:7)

Model属性是String-key对象 - 值对。没有表达式正在解决。当你这样做

model().attribute("emailInfo.email", "currentMember@example.com"));//

您询问是否存在具有指定值的键Model的{​​{1}}属性。答案显然是否定的,因为你没有带有这样一个密钥的emailInfo.email属性。

你拥有的是

Model

这是model.addAttribute("emailInfo", emailInfo); 属性,其中键Model包含类型为emailInfo的对象。您需要自己比较EmailInfo对象的email字段。