如何在java中对jdbc代码进行单元测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266370/
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-08-11 12:13:32  来源:igfitidea点击:

How do I unit test jdbc code in java?

javadatabaseunit-testingjdbcjunit

提问by ScArcher2

I'd like to write some unit tests for some code that connects to a database, runs one or more queries, and then processes the results. (Without actually using a database)

我想为一些连接到数据库的代码编写一些单元测试,运行一个或多个查询,然后处理结果。(没有实际使用数据库)

Another developer here wrote our own DataSource, Connection, Statement, PreparedStatement, and ResultSet implementation that will return the corresponding objects based on an xml configuration file. (we could use the bogus datasource and just run tests against the result sets it returns).

这里的另一个开发人员编写了我们自己的 DataSource、Connection、Statement、PreparedStatement 和 ResultSet 实现,它们将根据 xml 配置文件返回相应的对象。(我们可以使用伪造的数据源并只针对它返回的结果集运行测试)。

Are we reinventing the wheel here? Does something like this exist already for unit testing? Are there other / better ways to test jdbc code?

我们是在重新发明轮子吗?单元测试是否已经存在这样的东西?是否有其他/更好的方法来测试 jdbc 代码?

采纳答案by WMR

You could use DBUnittogether with a HSQLDBwhich can read its initial data from CSV files for example.

例如,您可以将DBUnitHSQLDB一起使用,后者可以从 CSV 文件中读取其初始数据。

回答by Steve K

There is DBUnit. It won't allow you to test your jdbc code without a database, but it seems like you could introduce a different set of buys by emulating a database.

DBUnit。它不允许你在没有数据库的情况下测试你的 jdbc 代码,但似乎你可以通过模拟数据库来引入一组不同的购买。

回答by S.Lott

That's why you have derby(now called JavaDB) or sqlite-- they are small, simple databases that you can create, load, test against and destroy relatively quickly and simply.

这就是为什么您有derby(现在称为 JavaDB)或sqlite—— 它们是小型、简单的数据库,您可以相对快速而简单地创建、加载、测试和销毁它们。

回答by Sunny Milenov

Use any of the Mock frameworks for such a task. (jMock, etc.)

使用任何 Mock 框架来完成这样的任务。(JMock的等等)。

Some examples

一些例子

回答by user30684

I would say that HSQL is the way to go during your unit tests. The point of your test is to test your jdbc code and make sure it works. Adding custom classes or mocking the jdbc calls can easily hide bugs.

我会说 HSQL 是单元测试期间要走的路。测试的重点是测试 jdbc 代码并确保它有效。添加自定义类或模拟 jdbc 调用可以轻松隐藏错误。

I mostly use mysql and when the tests run the driver class and url is changed to org.hsqldb.jdbcDriver and jdbc:hsqldb:mem:test.

我主要使用 mysql,当测试运行驱动程序类时,url 更改为 org.hsqldb.jdbcDriver 和 jdbc:hsqldb:mem:test。

回答by P Arrayah

While the way to mock jdbc in your application is of course dependant on how you've implemented your actual jdbc transactions.

虽然在您的应用程序中模拟 jdbc 的方式当然取决于您如何实现实际的 jdbc 事务。

If you're using jdbc as is, I'd assume you have written yourself an utility class of sorts to do some tasks in the line of DBUtils.getMetadataFor(String tablename). What this would mean is that you'd have to create a mock of that class and that could be all you need. This would be rather easy solution for you since you apparently already have a series of jdbc related mock objects available. Note that I'm assuming your jdbc code isn't exploded all around the application - if it is, refactor!!!

如果您按原样使用 jdbc,我假设您已经为自己编写了一个实用程序类来执行DBUtils.getMetadataFor(String tablename). 这意味着您必须创建该类的模拟,这可能就是您所需要的。这对您来说是相当简单的解决方案,因为您显然已经有一系列与 jdbc 相关的模拟对象可用。请注意,我假设您的 jdbc 代码没有在应用程序周围爆炸 - 如果是,请重构!!!

If you're however using any framework for database handling (like Spring Framework's JDBC Template classes) you can and should mock the interface class using EasyMock or some other equivalent. That way you can have all the power in the world required for easy mocking of the connection.

但是,如果您使用任何用于数据库处理的框架(如 Spring Framework 的 JDBC 模板类),您可以并且应该使用 EasyMock 或其他等效物来模拟接口类。这样,您就可以拥有世界上轻松模拟连接所需的所有功能。

And last if nothing else works, you can do what others have said already and use DBUnit and/or derby.

最后,如果没有其他工作,您可以按照其他人已经说过的方法使用 DBUnit 和/或 derby。

回答by Tatu Lahtela

You have several options:

您有多种选择:

  • Mock the database with a Mock library, e.g. JMock. The huge drawback of this that your queries and the data will most likely be not tested at all.
  • Use a light weight database for the tests, such as HSQLDB. If your queries are simple, this is probably the easiest way to go.
  • Dedicate a database for the tests. DBUnitis a good option, or if you are using Maven, you can also use the sql-maven-pluginto set up and tear down the database properly (be careful of dependencies between tests). I recommend this option as it will give you the biggest confidence that the queries work properly with your db vendor.
  • 使用 Mock 库模拟数据库,例如JMock。这样做的巨大缺点是您的查询和数据很可能根本没有经过测试。
  • 使用轻量级数据库进行测试,例如HSQLDB。如果您的查询很简单,这可能是最简单的方法。
  • 为测试指定一个数据库。DBUnit是一个不错的选择,或者如果您正在使用 Maven,您也可以使用sql-maven-plugin正确设置和拆除数据库(注意测试之间的依赖关系)。我推荐这个选项,因为它会给你最大的信心,让你的查询与你的数据库供应商一起正常工作。

Sometimes it is necessary and useful to make these tests configurable so that these tests are only executed if the database is available. This can be done with e.g. build properties.

有时,使这些测试可配置是必要且有用的,以便仅在数据库可用时才执行这些测试。这可以通过例如构建属性来完成。

回答by folone

I prefer using EasyMockfor testing a not-so-easy-to-test code.

我更喜欢使用EasyMock来测试不太容易测试的代码

回答by grbonk

We use Mockrunner. http://mockrunner.sourceforge.net/It has mock connections and datasources built in so there is no need to implement them your selves.

我们使用 Mockrunner。http://mockrunner.sourceforge.net/它内置了模拟连接和数据源,因此您无需自己实现它们。

回答by Jim Hurne

I like to use a combination of:

我喜欢使用以下组合:

You can get pretty far with just DBUnit and HSQLDB. Unitils provides the last mile of code to manage and reset database state. It also provides a nice way of managing database schema changes and makes it easy to use specific RBDMS (Oracle, DB2, SQL Server, etc). Finally, Unitils provides some nice wrappers around DBUnit which modernizes the API and makes DBUnit much more pleasant to work with.

仅使用 DBUnit 和 HSQLDB 就可以走得很远。Unitils 提供了最后一英里的代码来管理和重置数据库状态。它还提供了一种管理数据库模式更改的好方法,并使使用特定的 RBDMS(Oracle、DB2、SQL Server 等)变得容易。最后,Unitils 为 DBUnit 提供了一些很好的包装器,它们使 API 现代化并使 DBUnit 使用起来更加愉快。

If you haven't checked out Unitils yet, you definitely should. Unitils is often overlooked and under-appreciated.

如果您还没有检查过 Unitils,那么您绝对应该检查一下。Unitils 经常被忽视和低估。