我可以在使用MockMvc的方法中检查@ModelAttribute作为此方法的参数?

时间:2013-10-16 09:03:32

标签: java testing spring-mvc mocking

我在类中标记为@Controller

的方法方法
@RequestMapping(value = "/addEvent", method = RequestMethod.POST)
    public String addEvent(Model model,
            @Valid @ModelAttribute("myEvent") Event event,
            BindingResult result, RedirectAttributes redirectAttributes,
            @RequestParam(required = true) Integer selectedEventTypeId,
            @RequestParam(required = true) Integer selectedEventStatusId) {

        if (result.getErrorCount() > 1 ){
            return "eventDetailsAdd";
        }
        eventService.addEvent(event, selectedEventTypeId, selectedEventStatusId);
        redirectAttributes.addAttribute("idEvent", event.getId());
        redirectAttributes.addAttribute("message", "added correctly at " + new Date() );
        return "redirect:eventDetails";
    }

使用MockMvc我可以检查什么?

更新

如果我写下面的代码:

MockHttpServletRequestBuilder request = MockMvcRequestBuilders
                .get("/addEvent");
        ResultActions result = mockMvc.perform(request);

        result.andExpect(MockMvcResultMatchers.status().isOk());

我明白了:

java.lang.AssertionError: Status expected:<200> but was:<405>
    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.StatusResultMatchers$5.match(StatusResultMatchers.java:549)
    ...

更新

我复制你的例子:

public class ModelAssertionTests {

        private MockMvc mockMvc;

        @Before
        public void setup() {

                SampleController controller = new SampleController("a string value", 3, new Person("a name"));

                this.mockMvc = standaloneSetup(controller)
                                .defaultRequest(get("/"))
                                .alwaysExpect(status().isOk())
                                .build();
        }


        @Test
        public void testHasErrors() throws Exception {
                mockMvc.perform(post("/persons")).andExpect(model().attributeHasErrors("person"));
        }




        @Controller
        private static class SampleController {

                private final Object[] values;

                public SampleController(Object... values) {
                        this.values = values;
                }

                @RequestMapping("/")
                public String handle(Model model) {
                        for (Object value : this.values) {
                                model.addAttribute(value);
                        }
                        return "view";
                }

                @RequestMapping(value="/persons", method=RequestMethod.POST)
                public String create(@Valid Person person, BindingResult result, Model model) {
                        return "view";
                }
        }

}
class Person{
    public Person(){}
    public Person(String name) {
        super();
        this.name = name;
    }

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

执行此代码后,我会看到下一条跟踪:

java.lang.AssertionError: No errors for attribute [person]
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:39)
    at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:72)
    at org.springframework.test.web.servlet.result.ModelResultMatchers$5.match(ModelResultMatchers.java:110)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
    at com.epam.hhsystem.web.controllers.ModelAssertionTests.testHasErrors(ModelAssertionTests.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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)

我很困惑

1 个答案:

答案 0 :(得分:1)

MockMvc将让您检查有关收到的响应的各种条件。您可以在http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework

找到一些信息

特别是您可能对以下课程感兴趣:http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/test/web/servlet/result/MockMvcResultMatchers.html

此类允许您访问可用于在状态,内容,标题,模型,视图等上声明条件的其他结果匹配器。

在您的情况下,您可以检查:

  • 状态为200 OK或者与重定向网址一起重定向
  • 已呈现正确的视图(如果错误计数大于1)
  • 事件有错误