如何模拟HSQL DB Junit的数据库会话

时间:2015-10-29 10:46:53

标签: java hibernate junit hsqldb

我有一个存储库实现类,如下所示。

public class TesepositoryImpl extends OTPRepositoryBase implements
        TestRepository {

    public TesepositoryImpl() {
        super(RequiresOTPSession.YES);
    }

    @Override
    public int createPreference(AirDetail AirDetail)
            throws PersistenceException {
        Transaction tx = null;
        Session session = null;
        try {
            session = otpDatabaseSession.openSession();
            tx = session.beginTransaction();
            session.save(AirDetail);
            tx.commit();
        } finally {
            otpDatabaseSession.closeSession(session);
        }
                return AirDetail.getAirDetailId().intValue();
    }

我已经说过为上述

撰写junit
public class TesRepositoryImplTest {


    private EmbeddedDatabase db;

    @Before
    public void setUp() {
        db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
                .addScript("db/sql/create-db.sql")
                .addScript("db/sql/insert-data.sql").build();


    }   

    TesRepository testRepository = new TestRepositoryImpl();

    @Test
    public void createPreference() throws Exception {

        //*setting values** creating preference**/

        int airRefereID = testRepository
                .createPreference(preference);

    }



    @After
    public void tearDown() {
        db.shutdown();
    }

我将hibernate cfg文件定义如下

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
                <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:mem:fcds</property>
        <property name="connection.username">SA</property>
        <property name="connection.password"></property>

        <property name="connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="connection.autocommit">true</property>

        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>

        <property name="hibernate.current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.standard_service_registry_builder">false</property>
        <mapping class="com.aa.dotc.commons.domain.preference" />
    </session-factory>
</hibernate-configuration>

我需要知道以下内容。

当我调用testRepository时                 .createPreference(偏好);

我需要将otpsession覆盖到HSQL会话。

请帮助我如何使用HSQL实现上述目的或共享任何样本以实现相同目的。

0 个答案:

没有答案