2015-10-15 69 views
0

我想爲我的應用程序設置一些單元測試,我想測試Unitils數據集,但到目前爲止我遇到了很多問題。我確信我有錯誤的設置可以讓任何人查看它的問題。無法單元測試 - Unitils,休眠

我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration 
    PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 
     <!-- database connection settings --> 
     <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> 
     <property name="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</property> 

     <property name="hibernate.connection.url">jdbc:hsqldb:mem:testdb</property> 
     <!--<property name="hibernate.hbm2ddl.auto">create</property>--> 



     <!-- Enable Hibernate's second level cache --> 
     <property name="hibernate.cache.use_second_level_cache">false</property> 
     <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property> 

     <!-- helper debug settings --> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.format_sql">true</property> 

     <mapping class="cz.test.Request"/> 
    </session-factory> 

</hibernate-configuration> 

Unitils特性如下

database.dialect=hsqldb 
database.driverClassName=org.hsqldb.jdbc.JDBCDriver 

database.url=jdbc:hsqldb:mem:testdb 
database.schemaNames=PUBLIC 

最後我的測試是

@DataSet("DaoTest.xml") 
@HibernateSessionFactory({"test_hibernate.cfg.xml"}) 
public class DaoTest extends UnitilsJUnit4 { 

    private SessionFactory sessionFactory; 

    private Session session = null; 

    @Test 
    public void testMappingToDatabase() { 
     HibernateUnitils.assertMappingWithDatabaseConsistent(); 
    } 
} 

我越來越

SEVERE: Table 'TrnRequest' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false] 
Caused by: org.dbunit.dataset.NoSuchTableException: Request 

我猜是不是創建的模式,設置在hibernate.cfg.xml東西作爲創建的這個錯誤後進行,並與

X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: exporting generated schema to database 
X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport create 
SEVERE: Unsuccessful: create table Request (id bigint generated by default as identity (start with 1), amount bigint, amountCbk bigint, applicationId varchar(4), batch integer, cardIssuer varchar(20), cardSeq integer, currency varchar(3), emvData varchar(1024), entryMode varchar(3), exp varchar(4), maskedPan varchar(32), originalTime timestamp, ORIGINALTIME_UTC timestamp, pan varchar(64), panKey varchar(64), posData varchar(512), repeated NUMBER(1,0) DEFAULT 0 NOT NULL, reqFlag varchar(255), reqProcessing varchar(255), reqType varchar(255), requestTime timestamp, REQUESTTIME_UTC timestamp, sequenceNumber varchar(10), stan bigint, termid varchar(16), transactionTime timestamp, primary key (id)) 
X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport create 
SEVERE: type not found or user lacks privilege: NUMBER 
X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport create 
SEVERE: Unsuccessful: alter table TrnReleatedTrn add constraint FK135128224355CCD7 foreign key (releatedRequest_id) references TrnRequest 
X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport create 
SEVERE: user lacks privilege or object not found: REQUEST 
X 15, 2015 9:44:19 DOP. org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: schema export complete 
+0

[這裏](http://jonathan.lalou.free.fr/?tag=table-not-found-in-tablemaporg-dbunit-dataset-orderedtablenamemap_tablenames)我發現「事實上,即使你提供了一個數據集t通過一個扁平的XML文件,DBUnit不會創建這些表格,但只能填充它們「。不錯,但是我想在內存模式中使用,並且在hibernate.cfg.xml中創建 create將僅在以後執行創建。 – Zveratko

回答

1

反正失敗,沒有幫助所以最後我做到了工作 測試現在看起來像

public class SettlementDaoTest extends UnitilsJUnit4{ 

    @HibernateSessionFactory("test_hibernate.cfg.xml") 
    SessionFactory hsfLocal; 

    @Before 
    public void before(){ 
     SessionFactoryProvider.setSessionFactory(hsfLocal); 
    } 

    @Test 
    @DataSet 
    public void testGetTransaction() throws Exception { 


     SessionFactoryProvider.getSessionFactory().getCurrentSession().beginTransaction(); 

     List<Long> customerIds = Factory.getAtFileSettlementDao().getCustomerIds("TB01"); 
     Assert.assertNotNull(customerIds); 
     Assert.assertEquals(4924L , (long)customerIds.get(0)); 
     SessionFactoryProvider.getSessionFactory().getCurrentSession().getTransaction().commit(); 


    } 
} 

我unitils.properties更改的設置 unitils.module.dbunit.runAfter =春天 - 這DATAS推遲加載ETS後的架構是根據

<property name="hibernate.hbm2ddl.auto">create-drop</property> 

DatabaseModule.Transactional.value.default創建=禁用這在某種程度上禁用事務僵局,這可能是造成的,因爲我使用的是

<property name="hibernate.current_session_context_class">thread</property> 

DatabaseModule.Transactional.value.default =禁用