使用模拟数据库进行JUnit测试 [英] JUnit tests using a mock database

查看:183
本文介绍了使用模拟数据库进行JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个测试不同WebServices的应用程序,并且希望它尽可能通用。我需要填充数据库以进行JUnit测试,但是我不想提交这些更改。
我知道某些内存数据库(例如HSQL DB)可以在某种虚拟(或模拟)数据库上进行测试,但是不幸的是,由于我使用了复杂的数据表结构,因此我使用了oracle,现在无法更改它。

您建议的最佳做法是什么?
谢谢。

解决方案

首先,HSQL和Hibernate毫无关联。问题是您是否可以找到一个支持与生产数据库相同的SQL(或应用程序使用的SQL子集)的嵌入式数据库。



一个不错的候选人因为这是 H2数据库,因为它模拟了许多不同的SQL风格。



最重要的是:不要测试数据库。假设您的供应商对数据库进行了全面的测试,并且可以正常工作。



在我的代码中,我的目标是:


  1. 保存并加载每个实体。


  2. 为我使用的所有查询生成SQL并与之进行比较测试中的字符串文字(即,我不会一直对数据库运行查询)。


  3. 某些测试会查找System属性。如果已设置,则他们将对数据库运行查询。


这样做的理由是:只要数据库模式不更改时,没有必要实际运行查询。这意味着在我坐在电脑前的白天运行它们会浪费大量时间。



为确保低影响更改不会打滑通过差距,当我不在乎时,我让计算机运行它们。



沿着同一行,我有许多DAO的模拟,它们返回了各种预定义的结果,因此我不必查询数据库。这里的基本原理是我要测试数据库中结果的处理,而不是JDBC API,DB驱动程序,OS的TCP / IP堆栈,网络硬件(和软件)或我之间的1000项中的任何其他项代码和数据库记录在某处的硬盘上。



我的博客中有更多详细信息: http://blog.pdark.de/2008/07/26/testing-with-databases/


I am developping an application that tests different WebServices, and I want it to be as generic as possible. I need to populate database to do JUnit tests, but I don't want these changes to be commited. I know that some in-memory databases like HSQL DB allow testing on a sort of a virtual (or mock) database, but unfortunately I use oracle and I cannot change it now because of my complex data tables structure.

What is the best practice you suggest? Thanks.

解决方案

First of all, HSQL and Hibernate aren't related in any way. The question is whether you can find an embedded database which supports the same SQL as your production database (or rather the subset of SQL which your application uses).

A good candidate for this is H2 database since it emulates a lot of different SQL flavours.

On top of that: Don't test the database. Assume that the database is tested thoroughly by your vendor and just works.

In my code, I aim for:

  1. Save and load each entity.

  2. Generate the SQL for all the queries that I use and compare them against String literals in tests (i.e. I don't run the queries against the database all the time).

  3. Some tests look for a System property. If it's set, then they will run the queries against the database. This happens during the night on my CI server.

The rationale for this: As long as the DB schema doesn't change, there is no point to actually run the queries. That means running them during the day while I sit in front of the computer is a huge waste of time.

To make sure that "low impact" changes don't slip through the gaps, I let a computer run them when I don't care.

Along the same lines, I have mocks for many DAOs which return various predefined results, so I don't have to query the database. The rationale here is that I want to test the processing of results from the database, not the JDBC API, the DB driver, the OS's TCP/IP stack, the network hardware (and software), or any other of the 1000 things between my code and the database records on a harddisk somewhere.

More details in my blog: http://blog.pdark.de/2008/07/26/testing-with-databases/

这篇关于使用模拟数据库进行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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