Spring Test DBUnit:无法从文件加载数据集

Posted

技术标签:

【中文标题】Spring Test DBUnit:无法从文件加载数据集【英文标题】:Spring Test DBUnit: Unable to load dataset from file 【发布时间】:2015-07-19 11:31:23 【问题描述】:

我正在尝试使用 Spring Test DBUnit 运行集成测试,以检查 DAO 的服务是否正常运行。对于两个相似的实体,我能够创建运行正常的测试,但对于这个特定的实体,测试无法正常运行。 测试将被忽略,我将在控制台中看到的唯一异常是:

java.lang.IllegalArgumentException: Unable to load dataset from "data/offline_message.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader

这里是相关文件。 XML 文件:

<dataset>

    <mp_account id="1" auth_hash="ted.mosby" first_name="Ted" last_name="Mosby" credential="EMAIL" transport_session="someTransportSession"/>
    <mp_account id="2" auth_hash="lily.aldrin" first_name="Lily" last_name="Aldrin" credential="MEH" transport_session="someTransportSession"/>

    <mp_message id="1" recipient_account_id="1" sender_account_id="2"/>

</dataset>

失败的测试类:

import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.somecompany.messaging.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Date;
import java.util.List;

@DatabaseSetup("data/message.xml")
public class MessageDaoTest extends AbstractDaoTest<Message> 

    private static final Long ACCOUNT_ID = 1L;
    public static final long DATE_LONG = 1431018764154L;
    private static final Date LAST_UPDATE_TS = new Date(DATE_LONG);
    @Autowired
    MessageDao MessageDao;

    @BeforeMethod
    public void setUp() throws Exception 
        this.setDao(MessageDao);
    

    @Test
    @Transactional
    public void testFindMessages() throws Exception 
        List<Message> Messages = this.MessageDao.findMessages(ACCOUNT_ID, LAST_UPDATE_TS);
        Assert.assertNotNull(Messages);
        Assert.assertEquals(Messages.size(), 1);
    


抽象测试类,继承自 TestNG 的类:

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

@TestExecutionListeners( DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class )
@ContextConfiguration(locations =  "classpath:test-context.xml" )
public class AbstractDaoTest <T> extends AbstractTestNGSpringContextTests 

    private GenericDao<T> dao;

    @Transactional
    public T create(T t)
        return dao.create(t);
    

    @Transactional
    public void delete(Object id)
        dao.delete(id);
    

    @Transactional
    public T find(Object id)
        return dao.find(id);
    

    @Transactional
    public T update(T t)
        return dao.update(t);
    

    public void setDao(GenericDao<T> dao) 
        this.dao = dao;
    

最后,实体:

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.persistence.*;

@NamedQueries(
        @NamedQuery(
                name = "findMessagesQuery",
                query = "select om from Message om where om.recipientAccount.id=:accountId " +
                        "and om.lastUpdatedTs>:time and om.lastUpdatedTs+om.expirationPeriod>:now " +
                        "order by om.lastUpdatedTs asc"
        ),
        @NamedQuery(
                name = "findExpiredMessagesQuery",
                query =  "select om from Message om where om.lastUpdatedTs+om.expirationPeriod<:now"
        )
)
@Entity
@Table(name="mp_message")
public class Message extends AbstractModel 

    @JoinColumn(name="recipient_account_id", updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Account recipientAccount;

    @JoinColumn(name="sender_account_id", updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Account senderAccount;

    @Column(name="message_body", length=2048)
    private String messageBody;

    @Column(name="expiration_period")
    private Long expirationPeriod;

    // Getters, setters, etc excluded for brevity

只是一个旁注:我已经“模糊”了实体名称(该死的律师!),所以可能会有一些小的名称错误。请多多包涵;) 如果您需要更多详细信息,请告诉我。 提前致谢

【问题讨论】:

尝试在你的路径之前添加classpath:,所以:@DatabaseSetup("classpath:data/message.xml") 谢谢!!将其发布为答案,我会接受它 【参考方案1】:

尝试在你的路径之前添加 classpath:,所以:

@DatabaseSetup("classpath:data/message.xml")

【讨论】:

只是一个小提示:在我的其他测试中,我不需要在路径前添加“classpath:”。有点奇怪,但是对于这个特殊的类,没有这个前缀是行不通的【参考方案2】:

只要这样做,它就可以工作。可以使用相对路径。

@DatabaseSetup("/data/message.xml"")

【讨论】:

以上是关于Spring Test DBUnit:无法从文件加载数据集的主要内容,如果未能解决你的问题,请参考以下文章

Spring、Hibernate、TestNG 和 h2:org.dbunit.dataset.NoSuchTableException:用户

数据库测试工具,谁更锋利?

在 HSQLDB 上使用 Spring DBUnit 进行休眠和 Spring 数据 - 由于外键约束而无法删除

春季测试 DBunit 警告

Spring Test:无法从 import.sql 文件将十六进制值插入 hsqldb

诚意租房网blog3