我正在尝试使用春季测试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")
就这样做,它起作用。可以使用相对路径。
@DatabaseSetup("/data/message.xml"")