Reputation: 852
I try to write some unit tests with DBUnit. I want to use the in-memory database HSQL. First in my Hibernate properties I use
<prop key="hibernate.hbm2ddl.auto">create</prop>
to create the tables from my entities.
Then I configure my datasource like this (file test-hibernate-config.xml):
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:mem:test;sql.syntax_ora=true</value>
</property>
<property name="username">
<value>SA</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
And then my test class look like this:
@Override
protected String[] getConfigLocations() {
return new String[] {"batch-spring-daos.xml",
"test-hibernate-config.xml",
"batch-spring-transactionManager.xml"};
}
public void setLivraisonDAO(ILivraisonDAO livraisonDAO) {
this.livraisonDAO = livraisonDAO;
}
@Test(expected=IllegalArgumentException.class)
@InsertDBUnitData(dataLocations={ "data/data_release" } )
public void shouldThrowIllegalArgumentExceptionWhenReleaseDoesntExists(){
livraisonDAO.getAllLivraisons("1");
}
@Test
@InsertDBUnitData(dataLocations={ "data/data_release", "data/data_livraison"} )
public void shouldRetrieveOneRelease(){
List<LivraisonEntity> allLivraisons = livraisonDAO.getAllLivraisons("2.5.0.0");
assertTrue(!allLivraisons.isEmpty());
}
The file batch-spring-dao.xml and batch-spring-transactionManager.xml are those that I use in my application.
But when I try to run my test, I got those logs:
[2014-11-03 10:06:41,349] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:348 : Unsuccessful: alter table IMPACTPARAMDEFECT add constraint FK7327F18F2A1C61A foreign key (IDPARAMETRAGE) references PARAMETRAGE
[2014-11-03 10:06:41,349] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:349 : a FOREIGN KEY constraint already exists on the set of columns: FK7327F18F2A1C61A in statement [alter table IMPACTPARAMDEFECT add constraint FK7327F18F2A1C61A foreign key (IDPARAMETRAGE) references PARAMETRAGE]
[2014-11-03 10:06:41,349] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:348 : Unsuccessful: alter table IMPACTPARAMDEFECT add constraint FK7327F1848CCEB7F foreign key (IDPARAMETRAGE) references PARAMETRAGE
[2014-11-03 10:06:41,364] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:349 : a FOREIGN KEY constraint already exists on the set of columns: FK7327F1848CCEB7F in statement [alter table IMPACTPARAMDEFECT add constraint FK7327F1848CCEB7F foreign key (IDPARAMETRAGE) references PARAMETRAGE]
[2014-11-03 10:06:41,364] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:348 : Unsuccessful: alter table IMPACTPARAMDEFECT add constraint FK7327F18DAE59D90 foreign key (IDPARAMETRAGE) references PARAMETRAGE
[2014-11-03 10:06:41,364] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:349 : a FOREIGN KEY constraint already exists on the set of columns: FK7327F18DAE59D90 in statement [alter table IMPACTPARAMDEFECT add constraint FK7327F18DAE59D90 foreign key (IDPARAMETRAGE) references PARAMETRAGE]
[2014-11-03 10:06:41,364] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:348 : Unsuccessful: alter table IMPACTPARAMDEFECT add constraint FK7327F188951F74F foreign key (IDPARAMETRAGE) references PARAMETRAGE
[2014-11-03 10:06:41,364] ERROR [main] org.hibernate.tool.hbm2ddl.SchemaExport.create:349 : a FOREIGN KEY constraint already exists on the set of columns: FK7327F188951F74F in statement [alter table IMPACTPARAMDEFECT add constraint FK7327F188951F74F foreign key (IDPARAMETRAGE) references PARAMETRAGE]
3 nov. 2014 10:06:41 org.springframework.orm.hibernate3.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@67966796] of Hibernate SessionFactory for HibernateTransactionManager
[2014-11-03 10:06:41,442] INFO [main] ch.gma.commons.test.AbstractGmaTest.insertData:183 : insertData - *** Inserting test data : file path = data/data_release, schema = ***
[2014-11-03 10:06:41,458] WARN [main] org.dbunit.database.DatabaseConnection.validateSchema:195 : The given schema 'SA' does not exist.
[2014-11-03 10:06:41,489] ERROR [main] org.dbunit.database.DatabaseDataSet.getTableMetaData:286 : Table 'RELEASE' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
[2014-11-03 10:06:41,489] INFO [main] ch.gma.commons.test.AbstractGmaTest.insertData:189 : insertData - *** Finished inserting test data ***
[2014-11-03 10:06:41,489] INFO [main] ch.gma.commons.test.AbstractGmaTest.insertData:183 : insertData - *** Inserting test data : file path = data/data_release, schema = ***
[2014-11-03 10:06:41,489] WARN [main] org.dbunit.database.DatabaseConnection.validateSchema:195 : The given schema 'SA' does not exist.
[2014-11-03 10:06:41,505] ERROR [main] org.dbunit.database.DatabaseDataSet.getTableMetaData:286 : Table 'RELEASE' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
[2014-11-03 10:06:41,505] INFO [main] ch.gma.commons.test.AbstractGmaTest.insertData:189 : insertData - *** Finished inserting test data ***
Thanks for your help!
Upvotes: 0
Views: 1887
Reputation: 852
Finally, I found the answer by myself.
I had to specify the PUBLIC schema to be used.
I add this property:
<prop key="hibernate.default_schema">PUBLIC</prop>
and to my unit tests, I changed the annotation:
@InsertDBUnitData(dataLocations={ "data/data_release" } )
to
@InsertDBUnitData(dataLocations={ "data/data_release" }, schema="PUBLIC")
And my tests work!
Hope this can help somebody!
Upvotes: 3