使用 Spring 进行单元测试与集成测试 [英] Unit tests vs integration tests with Spring

查看:38
本文介绍了使用 Spring 进行单元测试与集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Spring MVC 项目,并且我对源代码树中的所有各种组件进行了单元测试.

I'm working on a Spring MVC project, and I have unit tests for all of the various components in the source tree.

例如,如果我有一个控制器HomeController,它需要注入一个LoginService,然后在我的单元测试中HomeControllerTest我只是像往常一样(在 Spring 之外)实例化对象并注入属性:

For example, if I have a controller HomeController, which needs to have a LoginService injected into it, then in my unit test HomeControllerTest I simply instantiate the object as normal (outside of Spring) and inject the property:

protected void setUp() throws Exception {
    super.setUp();
    //...
    controller = new HomeController();
    controller.setLoginService( new SimpleLoginService() );
    //...
}

这对于将每个组件作为一个独立的单元进行测试非常有用——除了现在我在项目中有几十个类,在编写了一个类并为它编写了一个成功的单元测试之后,我一直忘记更新我的 Spring MVC 上下文在部署的应用程序中进行实际连接的文件.我发现我在将项目部署到 Tomcat 时忘记更新上下文文件,并从未连接的 bean 中找到了一堆 NullPointers.

This works great for testing each component as an isolated unit - except now that I have a few dozen classes in the project, after writing a class and writing a successful unit test for it, I keep forgetting to update my Spring MVC context file that does the actual wiring-up in the deployed application. I find out that I forgot to update the context file when I deploy the project to Tomcat and find a bunch of NullPointers from non-wired-up beans.

那么,这是我的问题:

  1. 这是我的第一个 Spring 项目 - 像我所做的那样为单个 bean 创建单元测试是否正常,然后创建第二套测试(集成测试)来测试一切是否按预期工作实际的应用环境?对此是否有既定的最佳做法?

  1. This is my first Spring project - is it normal to create unit tests for the individual beans, as I have done, and then create a second suite of tests (integration tests) to test that everything works as expected with the actual application context? Is there an established best practice for this?

此外,您如何将单元测试与集成测试分开?我在 src 中有所有源代码,test 中有单元测试 - 是否应该有第二个测试文件夹(例如 test-integration) 用于集成测试用例?

In addition, how do you separate the unit tests from the integration tests? I have all of the source code in src, the unit tests in test - should there be a 2nd test folder (such as test-integration) for integration test cases?

由于这是我的第一个 Spring 项目,我很好奇其他人通常是如何做这种事情的 - 与其重新发明轮子,不如问问社区的其他人.

Since this is my first Spring project, I'm curious how others usually go about doing this sort of thing - and rather than re-invent the wheel I rather ask the rest of the community.

推荐答案

我不能说是最佳实践,但这是我过去所做的.

I can't speak to being a best practice, but here's what I've done in the past.

单元测试:

  • 为重要的 bean(即大多数与 Spring 相关的 bean)创建单元测试
  • 在可行的情况下(即,大多数情况下,如果不是所有时间)将 Mock 用于注入的服务.
  • 对项目 test 目录中的这些测试使用标准命名约定.使用 TestTestCase 作为类名的前缀或后缀似乎被广泛使用.
  • Create unit tests for non-trivial beans (ie, most of your Spring related beans)
  • Use Mocks for injected services where practical (ie, most if not all the time).
  • Use a standard naming convention for these tests in the project test directory. Using Test or TestCase as a prefix or suffix to the classname seems to be widely practiced.

集成测试:

  • 创建一个 AbstractIntegrationTestCase 来设置一个 Spring WebApplicationContext 以在集成测试类中使用.
  • test 目录中使用集成测试的命名约定.我使用 IntTestIntegrationTest 作为这些测试的前缀或后缀.
  • Create an AbstractIntegrationTestCase that sets up a Spring WebApplicationContext for use in intetgration test clases.
  • Use a naming convention for integration tests in the test directory. I've used IntTest or IntegrationTest as a prefix or suffix for these tests.

设置三个 Ant test 目标:

Set up three Ant test targets:

  1. test-all(或任何你想命名的):运行单元和集成测试
  2. test:运行单元测试(仅仅因为 test 似乎是单元测试最常见的用法
  3. test-integration:运行集成测试.
  1. test-all (or whatever you want to name it): Run Unit and Integration Tests
  2. test: Run Unit tests (just because test seems to be the most common usage for unit testing
  3. test-integration: run the integration tests.

如前所述,您可以使用对您的项目有意义的命名约定.

As noted, you can use the naming conventions that make sense for your project.

关于将单元与集成测试分离到一个单独的目录中,我认为只要开发人员和他们的工具可以轻松找到并执行它们.

As to separating unit from integration tests into a separate directory, I don't think it matters as long as the developers and their tools can find and execute them easily.

举个例子,我使用 Spring 的最后一个 Java 项目完全使用了上面描述的内容,集成测试和单元测试位于同一个 test 目录中.另一方面,Grails 项目在通用测试目录下明确分离单元和集成测试目录.

As an example, the last Java project I worked on with Spring used exactly what is described above, with integration tests and unit tests living in the same test directory. Grails projects, on the other hand, explicitly separate unit and integration test directories under a general test directory.

这篇关于使用 Spring 进行单元测试与集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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