samedi 19 novembre 2016

DAO layer testing strategy with JUnit and Spring

I'm trying to devise an optimal strategy to unit-test DAO layer of my Spring app. Many existing approaches like in-memory DB usage, etc (posts: 12289800, 12390813, 9940010, 12801926). do not appeal to me.

So, here is a straightforward way that occurs to me:

  1. Create Spring test-context.xml and put there all the data needed for testing all the DAO classes;
  2. For each test class create a template method to test CRUD operations and all 'select' operations;
  3. Before testing, insert all needed data from test-context.xml to your real DB. We may need also some dependencies, so insert them as well, let's say in @Before method.
  4. After all CRUD operations, delete all dependencies from DB, let's say in @After method.

If we have a lot of dependencies, this may become a terribly expensive and laborious approach. Also we have only one test method (template method, to ensure the order of operations: create, read... etc.) - so one test per test class.

So, I need an advice whether this strategy is viable? What similar did you do to test your DAOs?

Aucun commentaire:

Enregistrer un commentaire