如何在java中单元测试jdbc代码? [英] How do I unit test jdbc code in java?

查看:551
本文介绍了如何在java中单元测试jdbc代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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).

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

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

推荐答案

//dbunit.sourceforge.net/rel =nofollow noreferrer> DBUnit 以及 HSQLDB 它可以从CSV文件中读取其初始数据。

You could use DBUnit together with a HSQLDB which can read its initial data from CSV files for example.

这篇关于如何在java中单元测试jdbc代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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