Mar 19th, 2011, 06:55 AM
Transaction behaviour within Unit Testing
after having read the reference docs and doing some testing by myself I am kind of lost concerning the transaction behaviour of Spring Batch within Junit tests.
Obviously, with Spring Batch setting of commit or rollback commands within the chunks we are not able to rollback when ending the unit tests. In order to execute tests not only once I would prefer to "leave nothing but footprints" after a suite run. Cleaning up by using @After annotation seems to be risky, since failing appropriate maintaining of the sweeper code may result in creating inconsistencies.
Therefore I would be great if you could provide some "patterns" dealing with Spring Batch and Junit Tests.
Thanks in advance!
Mar 19th, 2011, 11:56 AM
I tend to clean the whole database and inject test data before each test method (for any kind of test, Spring Batch or not.) This can look brutal, but it works nicely. Cleaning doesn't take that long (10s ms, depending on the number of tables and the database.)
Mar 20th, 2011, 03:14 AM
Unless you don't rely on the fact that an existing job instance already existed or something like that, it's job specific I'd say.
If your job touches some business tables when it runs, you know about them in your test so I would create the necessary helpers to clean that just as you would build helper (or matcher) to assert that the job ran successfully.
Mar 31st, 2011, 07:45 AM
thank you for your replies. According to your suggestions, I now clean the database before an after executing tests.
Anyway: I am looking forward receiving addtional ideas.