使用仅返回视图名称的方法对 spring 3 控制器进行单元测试

发布于 2024-12-05 21:00:29 字数 280 浏览 1 评论 0原文

我的理解是,对 Spring 控制器进行单元测试的有效方法过于简单地实例化(所需控制器的)新实例并直接测试控制器方法。不用担心模拟请求等,因为 spring 本身确实需要测试。

但是,我的许多控制器方法只是将视图名称作为单个字符串返回,并且我想测试模型本身是否具有正确的参数和数据。

我是否需要更改方法以便它们返回 ModelAndView,以便我可以在单元测试中访问模型?我的方法是否应该这样做(返回模型和视图)?

当我在控制器方法中创建新的 ModelAndView 时,现有模型是否会被覆盖?

Its my understanding that an efficient way to unit test a spring controller is too simply instantiate a new instance (of the required controller) and test the controller methods directly. Not bothering with Mock requests etc, as spring itself does need testing.

However, many of my controller methods simply return a view name as a single String, and I want to test that the model itself has the correct parameters and data.

Do I need to change the methods so that they return a ModelAndView, so that I can then access the model within the unit test ? Should my methods be doing that (returning a model and view) anyway ?

When I create a new ModelAndView within a controller method does the existing Model get overwritten ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2024-12-12 21:00:29

我会尝试回答你的一些问题。

关于控制器的单元测试,有时我发现需要添加 Spring 的 Mock 请求和响应,因为某些 Spring 功能期望请求上下文中的请求和响应。

class MyTest {
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @BeforeMethod(alwaysRun = true)
    public void setup() {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        controller = new Controller( /*inject dependencies here*/ );
    }
}

这很容易弄清楚,因为如果它们不存在,您将收到一个非常具体的错误,指出请求或响应不在上下文中。

与模型相关,我发现返回 ModelAndView 并添加一些检查输出参数和视图名称的测试更容易。

public void shows_xxx_index_view {
    assertThat(modelAndView.getViewName(), equalTo("controller/index"));
}

public void model_contains_search_results {
    assertThat(modelAndView.getModelMap().get("searchResults"), equalTo(expected_results));
}

我不知道模型是否会被覆盖。

这是我在一个小项目中的经验,所以我绝不是专家,但我发现这种方法很有用。

I'll try to answer some of your questions.

About unit testing the controllers, sometimes I found that it's needed to add Spring's Mock request and response, as some spring features expect the request and response in the request context.

class MyTest {
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @BeforeMethod(alwaysRun = true)
    public void setup() {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        controller = new Controller( /*inject dependencies here*/ );
    }
}

This is easy to figure out, because if they are not there, you'll get a very specific error saying that the request or response are not in the context.

Related to the model, I found easier to return a ModelAndView and add some tests that check the output parameters and view name.

public void shows_xxx_index_view {
    assertThat(modelAndView.getViewName(), equalTo("controller/index"));
}

public void model_contains_search_results {
    assertThat(modelAndView.getModelMap().get("searchResults"), equalTo(expected_results));
}

And I don't know if the model gets overwritten.

This is my experience from a small project, so I'm by no means an expert, but I found this approach useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文