开发者

How do I efficiently test SQL/JPQL/HQL?

It is often said when unit testing to dont test the database as that is an integration test (see point 4).

However, SQL/JPQL/HQL encapsulate data store specific logic which is often in string format on how to access data. This free form string data access command can easily go 开发者_如何学运维wrong and hence needs to be tested.

How do i efficiently test this sort of logic?


The closest you can get to running a unit test against an SQL (or similar framework) query, is to set up a SQLite database in memory, and run against it.

While that still is technically an integration test, it runs almost as fast as a unit test should.

If you do so, just take care to note the slight differences between SQLite and your real database, and try to make your queries compatible with both.

Hope this helps,
Assaf.


It is not a unit test, but there is nothing with using a unit testing framework like Nunit to test your sql. But it IS important that you keep it separated from the real unit tests. Real unit tests are fast and does not communicate with the outside ... nor do they attempt to alter it by updates, deletes and inserts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜