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

查看:51
本文介绍了如何在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?

推荐答案

您可以将 DBUnitHSQLDB 例如可以从 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天全站免登陆