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

查看:498
本文介绍了使用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.

所以,这里是我的问题:

So, here are my questions:


  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?

由于这是我的第一个春季项目,我很好奇其他人通常会如何做这类事情 - 而不是重新发明轮子我宁愿问社区其他人。

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创建单元测试(即,你的大部分春天相关豆类)

  • 在实际情况下(即大多数情况下)使用Mocks进行注入服务。

  • 对这些使用标准命名约定项目 test 目录中的测试。使用测试 TestCase 作为类名的前缀或后缀似乎已被广泛使用。

  • 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 供用于intetgration test clases。

  • test 目录中使用集成测试的命名约定。我已经使用 IntTest IntegrationTest 作为这些测试的前缀或后缀。

  • 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 测试目标:

Set up three Ant test targets:


  1. test-all(或任何你想要的名字):运行单元和集成测试

  2. 测试:运行单元测试(仅仅因为测试似乎是单元测试的最常见用法

  3. 测试集成:运行集成测试。

  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项目,在o上另外,在一般测试目录下明确地分离单元和集成测试目录。

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天全站免登陆