集成测试将整个对象发布到Spring MVC控制器 [英] Integration Testing POSTing an entire object to Spring MVC controller

查看:167
本文介绍了集成测试将整个对象发布到Spring MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在集成测试spring mvc Web应用程序时,有没有办法在模拟请求上传递整个表单对象?我所能找到的就是将每个字段分别作为这样的参数传递:

Is there a way to pass an entire form object on mock request when integration testing a spring mvc web app? All I can find is to pass each field separately as a param like this:

mockMvc.perform(post("/somehwere/new").param("items[0].value","value"));

哪种形式适用于小型表格。但是如果我发布的对象变大了怎么办?如果我可以发布整个对象,它也会使测试代码看起来更好。

Which is fine for small forms. But what if my posted object gets larger? Also it makes the test code look nicer if I can just post an entire object.

具体来说,我想通过复选框测试多个项目的选择,然后发布它们。当然我可以测试发布单个项目,但我想知道..

Specifically I'd like to test the selection of multiple items by checkbox and then posting them. Of course I could just test posting a single item, but I was wondering..

我们使用spring 3.2.2并包含spring-test-mvc。

We're using spring 3.2.2 with the spring-test-mvc included.

我的表格模型如下所示:

My Model for the form looks something like this:

NewObject {
    List<Item> selection;
}

我试过这样的电话:

mockMvc.perform(post("/somehwere/new").requestAttr("newObject", newObject) 

给这样的控制器:

@Controller
@RequestMapping(value = "/somewhere/new")
public class SomewhereController {

    @RequestMapping(method = RequestMethod.POST)
    public String post(
            @ModelAttribute("newObject") NewObject newObject) {
        // ...
    }

但是对象将是空的(是的,我在测试之前填写了它)

But the object will be empty (yes I've filled it before in the test)

我找到的唯一可行解决方案是使用@SessionAttribute之类的这个:
集成测试Spring MVC应用程序:表单

The only working solution I found was using @SessionAttribute like this: Integration Testing of Spring MVC Applications: Forms

但我不喜欢havi的想法记得在我需要的每个控制器的末尾调用完成。在所有表单数据不必在会话中之后,我只需要它来处理一个请求。

But I dislike the idea of having to remember to call complete at the end of every controller where I need this. After all the form data does not have to be inside the session, I only need it for the one request.

所以我现在唯一能想到的就是编写一些使用MockHttpServletRequestBuilder的Util类,使用反射将所有对象字段附加为.param,或者为每个测试用例单独添加..

So the only thing I can think of right now is to write some Util class that uses the MockHttpServletRequestBuilder to append all the object fields as .param using reflections or individually for each test case..

我不知道,感觉不合适-intuitive ..

I don't know, feeld un-intuitive..

关于如何让我变得更轻松的任何想法/想法? (除了直接调用控制器)

Any thoughts / ideas on how I might make my like easier? (Apart from just calling the controller directly)

谢谢!

推荐答案

使用 MockMvc 进行集成测试的主要目的之一是验证模型对象是否与表单数据相关联。

One of the main purposes of integration testing with MockMvc is to verify that model objects are correclty populated with form data.

为了做到这一点,你必须传递表单数据,因为它们是从实际表单传递的(使用 .param())。如果您使用从 NewObject 到数据的自动转换,您的测试将不会涵盖特定类别的可能问题(修改 NewObject 与实际形式不符。)

In order to do it you have to pass form data as they're passed from actual form (using .param()). If you use some automatic conversion from NewObject to from data, your test won't cover particular class of possible problems (modifications of NewObject incompatible with actual form).

这篇关于集成测试将整个对象发布到Spring MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆