I made a mistake... it was a common newbie mistake, to be sure, but now I have to fix it. After looking at some example code (and I can't remember where, or who's example it was), I decided that transactions should be invoked around my DAO object, as opposed to my service objects. Whoops! My bad. Now I'm trying to correct that mistake before I write any more DAOs or Service objects, and I'm trying to figure out how to adjust my JUnit tests accordingly.
As it stands, to test the Service objects, I'm using Mock Objects of the DAOs, making sure the service objects pass the right parameters through to the DAO, and that the DAO result is used appropriately in the service object, and eventually returned to the JUnit test. For the DAO testing, I'm actually using a test database to ensure that the queries run, object creation, updates, deletes, and everything else happens as expected. In the JUnit tests, I've got my Transactions set up around the DAOs, just as it was set up for the actual program.
I would like to continue to test my service objects with Mock Objects, and only do database testing when I'm using DAOs directly. The problem is the transactions. If the service objects have the AOP interceptors for transactions, then I need to use a single persistence manager for each test that I run, and possibly one transaction. I just can't figure out how to use Swing's wiring to set up the transactions as part of the JUnit tests (say before setup(), test...(), and tearDown() are called).
So this leads me to ask, what can I do so the Spring wiring sets up transactions before each test function is called? Or is the best way to test service objects with transactions, and DAOs without to hook up to the database when testing service objects, and use mock objects for the DAOs?
And, now that I think about it, maybe a better question to ask is this:
When writing JUnit tests for service objects and DAOs, at which layer do you have the test functions go all the way down to the database, and at which layer is it more appropriate to use mock objects?


Reply With Quote