Damo Damo - 10 months ago 50
Java Question

EasyMock and Ibatis

In my DAO layer I am doing all my database work by calling stored procedures.
I was wondering has anyone been successful in testing their DAO layer using EasyMock?



I would say that's impossible. There's no way to assert (with EasyMock or other mocking framework) that the DAO actually called some stored procedure, verify what it did etc.

The only thing you can do with DAO + EasyMock is to mock/stub the DAO, but then you're not testing the DAO but instead the collaborator acting on the DAO (typically some kind of controller if we're speaking MVC).

To do integration test of DAO/StoredProcedures I recommend DBUnit:

  1. Put testdata into database (if you're using jUnit do this in @Before method)
  2. Call DAO method under test
  3. If method returned any result, compare this to expected data in (1)
  4. If method performed some inserts/updates, call a "read method" and compare result with (1)

In case your DAO provides CRUD for some business entity you can test each operation of your DAO:

  • testLoad - load from DB and compare with (1)
  • testInsert - insert new entity to DB then reload and compare
  • testUpdate - modify existing entity, save to DB and reload/compare
  • testDelete - delete some entity from (1), then try to load it and assert it fails (also good to check that nothing else was deleted)