简体   繁体   English

用于在Java中测试Service / DAO层的工具和方法

[英]Tools and Methods for testing Service/DAO layers in Java

I am trying to figure out the best way(s) to test Service and DAO layers. 我试图找出测试服务和DAO层的最佳方法。 So, a few sub questions... 那么,一些子问题......

  1. When testing a service layer, is it best to test against a mock DAO layer or a "live" DAO layer pointed at a testing environment? 在测试服务层时,最好是针对模拟DAO层或指向测试环境的“实时”DAO层进行测试吗?
  2. How should SQL in the DAO layer be tested when the only test database is in a shared environment (Oracle/DB2) 当唯一的测试数据库在共享环境中时,如何测试DAO层中的SQL(Oracle / DB2)
  3. How do you solve the paradox of any DAO writes/updates need to be tested with DAO reads which is something that also has to be tested? 你如何解决任何DAO写/更新的悖论需要使用DAO读取进行测试,这也需要进行测试?

I am looking for any good documentation, articles, or references in this area along with any tools to help automate the process. 我在这个领域寻找任何好的文档,文章或参考文献以及任何有助于自动化过程的工具。 I already know about JUint for unit testing and Hudson for CI. 我已经了解了JUint的单元测试和Hudson的CI。

Get Growing Object-Oriented Software, Guided by Tests . 在测试的指导下,获得不断增长的面向对象软件 It has some great tips about how to test database access. 它有一些关于如何测试数据库访问的好技巧。

Personally, I usually break the DAO tests in 2, a unit test with a mocked database to test functionality on the DAO, and an integration test, to test the queries against the DB. 就个人而言,我通常打破2中的DAO测试,使用模拟数据库进行单元测试以测试DAO上的功能,并进行集成测试,以测试针对数据库的查询。 If your DAO only has database access code, you won't need a unit test. 如果您的DAO只有数据库访问代码,则不需要进行单元测试。

One of the suggestions from the book that I took, is that the (integration) test has to commit the changes to the DB. 我所采用的书中的一个建议是(集成)测试必须将更改提交给DB。 I've learn to do this, after using hibernate and figuring out that the test was marked for rollback and the DB never got the insert statement. 我已经学会了这样做,在使用hibernate并确定测试标记为回滚并且DB从未得到插入语句之后。 If you use triggers or any kind of validation (even FKs) I think this is a must. 如果您使用触发器或任何类型的验证(甚至是FK),我认为这是必须的。

Another thing, stay away from dbunit, it's a great framwork to start working, but it becomes hellish when a project becomes something more than tiny. 另一件事,远离dbunit,开始工作是一个很好的框架,但当项目变得非常小时,它变得很糟糕。 My preference here, is to have a set of Test Data Builder classes to create the data, and insert it in the setup of the test or in the test itself. 我的偏好是拥有一组Test Data Builder类来创建数据,并将其插入测试设置或测试本身。

And check dbmigrate, it's not for testing, but it will help you to manage scripts to upgrade and downgrade your DB schema. 并检查dbmigrate,它不是用于测试,但它将帮助您管理脚本以升级和降级数据库架构。

In the scenario where the DB server is shared, I've creates one schema/user per environment. 在共享数据库服务器的场景中,我为每个环境创建了一个模式/用户。 Since each developer has his own "local" environment, he also owns one schema. 由于每个开发人员都有自己的“本地”环境,因此他还拥有一个模式。

Here are my answers : 以下是我的答案:

  1. Use mock DAOs to test your services. 使用模拟DAO来测试您的服务。 Much easier, mush faster. 更容易,更快。 Use EasyMock or Mockito or any other mock framework to test the service layer. 使用EasyMock或Mockito或任何其他模拟框架来测试服务层。
  2. Give each developer its own database schema to execute his tests. 为每个开发人员提供自己的数据库模式来执行他的测试 Such schemas are typically empty : the unit tests populate the database with a small test data set before running a test, and empties it once the test is completed. 这样的模式通常是空的:单元测试在运行测试之前使用小的测试数据集填充数据库,并在测试完成后清空它。 Use DBUnit for this. 为此使用DBUnit
  3. If the reads work against a well-defined, static, test data set (which you should unit-test), then you can rely on them to unit-test the writes. 如果读取对定义明确的静态测试数据集(您应该进行单元测试)起作用,那么您可以依赖它们对写入进行单元测试。 But you can also use ad-hoc queries or even DBUnit to test that the writes work as expected. 但您也可以使用即席查询甚至DBUnit来测试写入是否按预期工作。 The fact that the tests are not necessarily run in this order doesn't matter. 测试不一定按此顺序运行的事实并不重要。 If everything passes, then everything is OK. 如果一切都过去了,那么一切都好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM