I’m trying to integration test my application with Spring TestContext framework. I have done this by extending AbstractTransactionalJUnit4SpringContextTests, as usual. However, my application has three different data sources (with names like xDataSource, yDataSource, zdataSource), så when I try to run the test, the autowiring of data source in AbstractTransactionalJUnit4SpringContextTests won’t work, since it looks for a Data Source with autowire-by-type, but finds three, so it does not know which one to choose.
Is there any way to get Spring TestContext Framework to use three data sources? If so; how?
OK, I figured it out. The answer to this question is twofold. Firstly, extending
AbstractTransactionalJUnit4SpringContextTestswon’t work. This is because it needs a single data source for creating theSimpleJdbcTemplatefor verifying stuff with simple JDBC queries in the test. Since I don’t use this feature in this test, I could replaceextends AbstractTransactionalJUnit4SpringContextTestswith the collowing configuration:The combination of these annotations gives the same setup as extending
AbstractTransactionalJUnit4SpringContextTests.The second part was understanding that since I have three data sources, I also need all three so be referenced by the same
PlatformTransactionManager. I have distributed transactions. This is impossible with aDataSourceTransactionManager, so I had to use aJtaTransactionManager.