Spring 测试 DBUnit:无法从文件加载数据集
Spring Test DBUnit: Unable to load dataset from file
我正在尝试使用 Spring Test DBUnit 进行 运行ning 集成测试,检查 DAO 的服务是否 运行ning 正确。对于两个相似的实体,我能够创建 运行 好的测试,但是对于这个特定的实体,测试不能 运行 正确。
测试将被忽略,我将在控制台中看到的唯一异常是:
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>
测试 class 失败:
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);
}
}
抽象测试 class,从 TestNG 的 class 扩展而来:
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
}
只是一个旁注:我有 "obscured" 实体名称(该死的律师!),所以可能会有一些小的名称错误。请多多包涵 ;)
如果您需要更多详细信息,请告诉我。
提前致谢
尝试添加类路径:在你的路径之前,所以:
@DatabaseSetup("classpath:data/message.xml")
只要这样做,就可以了。可以使用相对路径。
@DatabaseSetup("/data/message.xml"")
我正在尝试使用 Spring Test DBUnit 进行 运行ning 集成测试,检查 DAO 的服务是否 运行ning 正确。对于两个相似的实体,我能够创建 运行 好的测试,但是对于这个特定的实体,测试不能 运行 正确。
测试将被忽略,我将在控制台中看到的唯一异常是:
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>
测试 class 失败:
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);
}
}
抽象测试 class,从 TestNG 的 class 扩展而来:
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
}
只是一个旁注:我有 "obscured" 实体名称(该死的律师!),所以可能会有一些小的名称错误。请多多包涵 ;)
如果您需要更多详细信息,请告诉我。
提前致谢
尝试添加类路径:在你的路径之前,所以:
@DatabaseSetup("classpath:data/message.xml")
只要这样做,就可以了。可以使用相对路径。
@DatabaseSetup("/data/message.xml"")