I'd like to be able to unit test my database during build. For this I have chosen the H2 database, as this should support DB2, which I'm currently using in production.
But I run into this error when running my tests:
All of my tests are fine and they should work with my H2 database as you'll see below (which is loaded later, this is the first logged in the console). As seen it loads applicationContext.xml, then it overrides the "dataSource" bean, which is defined as:Code:30-04-2012 09:28:57 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] 30-04-2012 09:28:57 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition INFO: Overriding bean definition for bean 'dataSource': replacing [Generic bean: class [org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]] with [Root bean: class [org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] ...
If I debug (e.g. through Eclipse) I can see that the url is substituted with "jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1", and I guess this is done by Spring when it overrides my "dataSource" bean.Code:<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> <constructor-arg> <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:mem:salgdb;MODE=DB2;DB_CLOSE_DELAY=-1" /> </bean> </constructor-arg> </bean>
If I create a break point and append ";MODE=DB2" (as defined by H2) all of my tests execute as expected (I use some DB2 specific SQL which doesn't work with the normal H2 base).
So, how do I fix this? Any ideas? E.g. why does it substitute the data source?
Here is my test, and as you can see I only load one application context:
Code:... //imports @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class AdvisorServiceDaoImplTest extends AbstractTransactionalJUnit4SpringContextTests { ...//setup @Test public void getAdvisor() throws Exception { //call db2 specific sql method assertEquals("1234567890", (advisorServiceDao.getAdvisor("123").getSsn())); } }


Reply With Quote
