开发者

How to organize data environment for JUnit tests?

I'm creating a data-intensive Java EE web application, which keeps its persistent data in a relational database. Now I want some JUnit tests to validate the application behavior in certain "data environments". Some objects should be created before tests and destroyed when tests are finished. Tests should be able 开发者_运维技巧to control their data setup configuration. Typical situation, huh? Now my questions are:

  • What tools to use for it? DBUnit looks rather old and is not JUnit-4 oriented.
  • Where to place and how to name object creators?
  • How to pass configuration details to them? Java call params? XML configs?
  • Shouldn't I use ExternalResource JUnit rule for convenience?

I'm interested to know what are the best practices. How do you solve this problem?


DBUnit is rather old and there is no more innovation in this project since 2009. The main problem is the heavy usage of XML for dataSet. Creation of dataset is hard but feasible. Maintenance is a nightmare and sometime too expensive...

If you use entities with an ORM framework you should try to use them directly for instantiating your database. I recommend Unitils for this, which help the configuration of the test database and provide the injection of JPA or Hibernate Session. You can combine this approach with the Builder Pattern to express the meaning of the datas and centralise the configuration.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜