Reset and Decouple Connections from resultsets
Brought to you by:
aibba
Using mockrunner with spring is problematic as the DAOs hold onto the datasource. Therefore every unit test uses the same datasource instance which can be dirtied before each test runs causing variable results.
I was wondering if there was a way to either (1) add a way to reset the MockConnection that is used by the datasource so it can be set/reset to a known configuration before each test. OR, (2) I'd like to see the result set information move out of the connection its self so that the connection is either independant of result set information or I can more easily preconfigure more complex result set info w/o regards to the connection.
Sorry. that was added by me.
You can always set a new and clean connection object using MockDataSource.setupConnection as a workaround. Decoupling would be a better solution, maybe in one of the future releases
Thanks for agreeing :-)
I was trying to reset the connection (ie. .setConnection(new MockConnection() ) in a @Before marked function. The unit tests we are running are transactional ( ie. AbstractTransactionalJUnit4SpringContextTests ) Therefore ( the problem ) spring grabs the connection to open a transaction before my '@Before' method.
Now of course I could use @DirtiesContext or a host of other ways to modify my tests but it seems like too many hoops. Perhaps moving the MockConnection<init> calls out of the constructor into a separate public method?
Thanks for your time.
I cannot promise when I find the time to have a closer look at this or when the next Mockrunner release will be. I agree that Mockrunner is to tightly coupled in this area (and other areas). However, working with singletons (and having to reset them) in JUnit tests is always a pain in my experience. This is always causing trouble and can lead to side effects even with a refactored decoupled Mockrunner. It's better to recreate everything in setUp/Before methods to have the same clean and well defined state for each test method. I think the way to go would be to redesign/configure your test/Spring environment not to use singleton connections, data sources or DAOs in the tests. If this is not possible you have to find a workaround. Perhaps reseting the ResultSetHandlers is enough. They have clear methods for everything, e.g. clearResultSets. You can also subclass MockDataSource to lazily create a new MockConnection on getConnection. In this case you have to subclass JDBCMockObjectFactory as well and override createMockDataSource to return your version. Just suggestions, I think there are many other ways.