【问题标题】:Unable to unit test - Unitils, Hibernate无法进行单元测试 - Unitils,Hibernate
【发布时间】:2016-01-13 13:42:45
【问题描述】:

我正在尝试为我的应用程序设置一些单元测试,我想使用 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

【问题讨论】:

  • here 我发现“确实,即使您通过平面 XML 文件提供数据集,DBUnit 也不会创建表,而只会填充它们”。很好,但我想在内存模式中使用并在 hibernate.cfg.xml 中设置 create 只会在之后执行创建。

标签: java hibernate unit-testing unitils


【解决方案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=spring - 这推迟了数据集的加载到根据

创建架构之后
<property name="hibernate.hbm2ddl.auto">create-drop</property>

DatabaseModule.Transactional.value.default=disabled 这以某种方式禁用了事务死锁,这可能是因为我正在使用

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

DatabaseModule.Transactional.value.default=disabled

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2020-01-15
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多