如何为数据库组件编写jUnit测试用例?

时间:2014-02-11 08:00:38

标签: java maven testing junit jenkins

我是jUnit的新手,有一个任务是为具有一些复杂数据库组件的应用程序编写jUnit测试用例。我可以为数据库组件编写测试用例,这些测试用例在我的本地maven构建创建中运行良好。但是在部署框中,我从jenkins maven命令创建构建时没有DB可用。请建议!!

4 个答案:

答案 0 :(得分:2)

jUnit有dbUnit扩展,旨在在测试运行之间创建一个已知的可重现状态。在CI框(以及本地)上使用内存数据库(如HSQL或H2)应该适合您。

干杯,

答案 1 :(得分:1)

通常的方法是在内存数据库环境中编写CRUD功能的单元测试。不完全是真实的东西,但不那么危险。看看

http://www.h2database.com/html/main.html

http://www.oracle.com/technetwork/articles/javase/javadb-10-5-142692.html

答案 2 :(得分:0)

您可以根据在真实数据库上运行的模拟和集成测试来分离单元测试的测试。仅在Jenkins中运行单元测试

答案 3 :(得分:0)

I am working same kind of project. In our application we use test schema and a reusable class which loads the data from the XML(File is same as test class) and Cache the data and removes the data from the table, so that Unit test get intended data. 
We set different profile and these are set via JVM arguments. And these profile have the database details and any other admin stuff required. In case of Junit to run we use the unit test profile.