So what’s the best way to stub out your data classes so that your business logic tests don’t break because they’re ultimately dependent on a test database over which you have little or no control? Well DbUnit’s been around for a while and TestNG has a DataProvider annotation that might be useful. But I haven’t found anything that uses the instrument package introduced in JDK1.5 despite the fact that it presents the opportunity for a really simple solution. The interface allows you to run some code prior to the main method and use that code to re-define classes. So you build against your actual data classes but execute your tests against replacements that don’t go anywhere near a database. Dive in by downloading this .zip file and looking at readme.txt; the structure’s covered in the diagram below.

I think this gives really good value: maximum control for minimal configuration and a few lines of code. I also think it slots in with any of the test frameworks you might already be using.