gpt4 book ai didi

Spring 测试 DBUnit : Unable to load dataset from file

转载 作者:行者123 更新时间:2023-12-03 20:19:00 25 4
gpt4 key购买 nike

我正在尝试使用 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")

关于Spring 测试 DBUnit : Unable to load dataset from file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com