我尝试用DBUnit编写一些单元测试。我想使用内存数据库HSQL。 首先在我的Hibernate属性中使用
<prop key="hibernate.hbm2ddl.auto">create</prop>
从我的实体创建表。
然后我像这样配置我的数据源(文件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>
然后我的测试类看起来像这样:
@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());
}
文件batch-spring-dao.xml和batch-spring-transactionManager.xml是我在我的应用程序中使用的文件。
但是当我尝试运行测试时,我得到了这些日志:
[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 ***
感谢您的帮助!
答案 0 :(得分:2)
最后,我自己找到了答案。
我必须指定要使用的PUBLIC架构。
我添加了这个属性:
<prop key="hibernate.default_schema">PUBLIC</prop>
和我的单元测试,我改变了注释:
@InsertDBUnitData(dataLocations={ "data/data_release" } )
到
@InsertDBUnitData(dataLocations={ "data/data_release" }, schema="PUBLIC")
我的测试工作正常!
希望这可以帮助别人!