0

我正在使用 Spring 3.1.0.RELEASE 和带有 Hibernate Provider 的 JPA 2 创建一个 Web 应用程序。我正在使用 junit 4.10、dbunit 2.4.8、unitils 3.3、hsqldb 2.2.8 进行测试。

我尝试测试服务层,创建操作。在我的 DAO 中,我有这个方法:

@Override
    @Transactional
    public void createQuestion(Question question) {
        logger.debug("createQuestion");

        entityManager.persist(question);
        logger.info("New question created  [id] {}", question.getId());

    }

我的 QuestionServiceTest 测试类:

@SpringApplicationContext("test-applicationContext.xml")
public class QuestionServiceTest extends UnitilsJUnit4 {

    @SpringBeanByName
    private QuestionService questionService;

    @SpringBeanByName
    private ThemeService themeService;

    @Test
    @DataSet("QuestionServiceTest.testCreateQuestion.xml")
    @ExpectedDataSet("QuestionServiceTest.testCreateQuestion-result.xml")
    public void testCreateQuestion() {
        final Question newQuestion = new Question();
        newQuestion.setCountryCode("FR");
        newQuestion.setEmail("test@mytest.com");
        newQuestion.setFirstName("FirstTest");
        newQuestion.setLastName("LastTest");
        newQuestion.setOriginalLang(LanguageEnum.FR);
        newQuestion.setOriginalQuestion("This is the original question");
        final Calendar calendar = Calendar.getInstance();
        calendar.set(2012, 5, 12);
        newQuestion.setCreationDate(calendar.getTime());
        final Theme theme = themeService.findThemeById(new Integer(1));
        newQuestion.setTheme(theme);
        questionService.createQuestion(newQuestion);
    }
}

我使用属性 hibernate.hbm2ddl.auto = create-drop 来生成模式,问题表是:

create table question (
        id integer generated by default as identity (start with 1),
        country_code varchar(10) not null,
        creation_date timestamp not null,
        email varchar(255) not null,
        firstname varchar(100) not null,
        lastname varchar(100) not null,
        original_lang varchar(255) not null,
        original_question clob not null,
        theme_id integer not null,
        primary key (id)
    )

theme_id 是表主题的外键。

当我使用 ExpectedDataSet 启动测试时,插入有效,但测试永远不会完成。上的测试块:

调试:org.dbunit.database.AbstractResultSetTable - 查询:从“ PUBLIC”.“QUESTION”按“ID”排序

这是调试的最后一行。

我的 unitils.properties 是:

# Defaults and other keys with explanations can be found there: http://unitils.org/unitils-default.properties
database.driverClassName=org.hsqldb.jdbcDriver
database.url=jdbc:hsqldb:mem:testOpen
database.userName=sa
database.password=
database.dialect=hsqldb

#  This schema is the initial schema when a new session is started in HSQLDB, don't change it or test won't works !
database.schemaNames=PUBLIC

dbUnit.datasetresolver.prefixWithPackageName=false
dbUnit.datasetresolver.pathPrefix=dataSets

我的 persistence.xml :

<persistence-unit name="OpenTestPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:testOpen" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>

我应该怎么办?我已经尝试过以前版本的 dbunit 或 unitils,但它并没有改变任何东西。预期数据集是一个非常酷的功能。

谢谢。

4

1 回答 1

0

我有同样的问题,但未能解决。然后我尝试了 spring-test-dbunit 并且这个库运行顺利。

于 2012-09-20T13:05:14.230 回答