如何单元测试SQL查询? [英] How to unit test an SQL query?
问题描述
我有一个类 DBHandler
,它接受一个查询,通过SQL服务器运行它,检查错误并返回结果。如何单元测试这个类?
I have a class DBHandler
which takes a query, runs it through the SQL server, checks for errors and returns the result. How can I unit test this class?
编辑:我会尝试更精确:
DBHandler
负责将查询传递到服务器。为了测试它实际上这样做,抛出正确的异常等,我想把它连接到一个模拟DB,我将填充。我的问题是 - 如何做到这一点?如何创建一个处理呼叫的模拟服务器?
Edit: I'll try to be more precise:
DBHandler
is in charge of passing the query to the server. In order to test that it actually does that, throws the correct exceptions, etc., I want to connect it to a mock DB which I will populate. My question is - how to do that? How can I create a mock "server" that handles calls?
推荐答案
只需传递SQL查询,并将返回的结果与预期结果进行比较。简单。 JUnit是一个单元测试框架,您可以利用它。
Just pass a SQL query, and compare the returned result to expected result. Simple. JUnit is a unit test framework, you can utilise that.
对于复杂的数据库单元测试,请查看 DBUnit 。
For sophisticated database unit testing, look at DBUnit.
这篇关于如何单元测试SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!