java 用于数据库代码的 JUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:50:09  来源:igfitidea点击:

JUnit for database code

javadatabaseunit-testingjunit

提问by shsteimer

I've been trying to implement unit testing and currently have some code that does the following:

我一直在尝试实现单元测试,目前有一些代码可以执行以下操作:

  1. query external database, loading into a feed table
  2. query a view, which is a delta of my feed and data tables, updating data table to match feed table
  1. 查询外部数据库,加载到提要表
  2. 查询视图,这是我的提要和数据表的增量,更新数据表以匹配提要表

my unit testing strategy is this:

我的单元测试策略是这样的:

I have a testing database that I am free to manipulate.

我有一个可以自由操作的测试数据库。

  1. in setUP(), load some data into my testing db
  2. run my code, using my testing db as the source
  3. inspect the data table, checking for counts and the existence/non existence of certain records
  4. clear testing db, loading in a different set of data
  5. run code again
  6. inspect data table again
  1. 在 setUP() 中,将一些数据加载到我的测试数据库中
  2. 运行我的代码,使用我的测试数据库作为源
  3. 检查数据表,检查计数和某些记录的存在/不存在
  4. 清除测试数据库,加载不同的数据集
  5. 再次运行代码
  6. 再次检查数据表

Obviously I have the data sets that I load into the source db set up such that I know certain records should be added,deleted,updated, etc.

显然,我有我加载到源数据库中的数据集,这样我就知道应该添加、删除、更新某些记录等。

It seems like this is a bit cumbersome and there should be an easier way? any suggestions?

好像这有点麻烦,应该有更简单的方法?有什么建议?

回答by David Carlson

Is it your intent to test the view which generates the deltas, or to test that your code correctly adds, deletes and updates in response to the view?

您的意图是测试生成增量的视图,还是测试您的代码是否正确添加、删除和更新以响应视图?

If you want to test the view, you could use a tool like DBUnitto populate your feed and data tables with various data whose delta you've manually calculated. Then, for each test you would verify that the view returns a matching set.

如果您想测试视图,您可以使用DBUnit 之类的工具来填充您的提要和数据表,其中包含您手动计算增量的各种数据。然后,对于每个测试,您将验证视图是否返回匹配集。

If you want to test how your code responds to diffs detected by the view, I would try to abstract away database access. I imagine an java method to which you can pass a result set (or list of POJO/DTO's) and returns a list of parameter Object arrays (again, or POJO's) to be added. Other methods would parse the diff list for items to be removed and updated. You could then create a mock result set or pojo's, pass them to your code and verify the correct parameters are returned. All without touching a database.

如果您想测试您的代码如何响应视图检测到的差异,我会尝试抽象出数据库访问。我想象了一个 java 方法,您可以向其中传递结果集(或 POJO/DTO 的列表)并返回要添加的参数对象数组(再次或 POJO 的)列表。其他方法将解析要删除和更新的项目的差异列表。然后,您可以创建一个模拟结果集或 pojo,将它们传递给您的代码并验证返回的参数是否正确。所有这些都无需接触数据库。

I think the key is to break your process into parts and test each of those as independently as possible.

我认为关键是将您的流程分解为多个部分,并尽可能独立地测试每个部分。

回答by Brian Matthews

DbUnitwill meet your needs. One thing to watch out for is that they have switched to using SLF4J as their logging facade instead of JCL. You can configure SLF4J to forward the logging to JCL but be warned if you are using Maven DbUnit sucks in their Nop log provider by default so you will have to use an exclusion, I bloggedabout this conflict recently.

DbUnit将满足您的需求。需要注意的一件事是,他们已转而使用 SLF4J 作为其日志记录外观,而不是 JCL。您可以将 SLF4J 配置为将日志记录转发到 JCL,但是如果您使用 Maven DbUnit 默认情况下在他们的 Nop 日志提供程序中很糟糕,那么您将被警告,因此您将不得不使用排除项,我最近在博客中讨论了这个冲突。

回答by Aaron

I use DbUnit, but also I work very hard to not to have to test against the DB. Tests that go against the database should only exist for the purpose of testing the database interface. So I have Mock Db Connections that I can set the data for use in all the rest of my tests.

我使用 DbUnit,但我也非常努力地工作,不必针对 DB 进行测试。针对数据库的测试应该只存在于测试数据库接口的目的。所以我有 Mock Db Connections,我可以设置数据以用于我所有其余的测试。

回答by Tatu Lahtela

If you are using Maven, one option is to use the sql-maven-plugin. It allows you to run database initialization/population scripts during the maven build cycle.

如果您使用 Maven,一种选择是使用sql-maven-plugin。它允许您在 Maven 构建周期中运行数据库初始化/填充脚本。

回答by Jeroen Heijmans

Apart from the already suggested DBUnit, you may want to look into Unitils. It uses DBUnit, but provides more than that (quoting from the site):

除了已经建议的 DBUnit 之外,您可能还想查看Unitils。它使用 DBUnit,但提供的还不止这些(引自网站):

  • Automatic maintenance of databases, with support for incremental, repeatable and post processing scripts
  • Automatically disable constraints and set sequences to a minimum value
  • Support for Oracle, Hsqldb, MySql, DB2, Postgresql, MsSql and Derby
  • Simplify test database connection setup
  • Simple insertion of test data with DBUnit * Run tests in a transaction
  • JPA entity manager creation and injection for hibernate, toplink and * Hibernate SessionFactory creation and session
  • Automatically test the mapping of JPA entities / hibernate mapped objects with the database
  • 自动维护数据库,支持增量、可重复和后处理脚本
  • 自动禁用约束并将序列设置为最小值
  • 支持 Oracle、Hsqldb、MySql、DB2、Postgresql、MsSql 和 Derby
  • 简化测试数据库连接设置
  • 使用 DBUnit 简单插入测试数据 * 在事务中运行测试
  • 用于 hibernate、toplink 和 * Hibernate SessionFactory 创建和会话的 JPA 实体管理器创建和注入
  • 自动测试 JPA 实体/休眠映射对象与数据库的映射