我的 dbUnit 测试之一遇到问题,无法解决。有一个数据结构,其具有由jpa注释定义的父子关系:
/**
* Parent account
*/
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parentAccountId", insertable = false, updatable = false)
private Account parentAccount;
/**
* Subordinated accounts
*/
@OneToMany(mappedBy="parentAccount")
@JsonIgnore
private Set<Account> childAccounts = new HashSet<Account>();
我有一个抽象类,它在执行 junit 测试之前加载数据库中的数据集,该类具有以下注释:
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@DatabaseSetup("classpath:db-test-script.xml")
@DatabaseTearDown(type=DatabaseOperation.DELETE_ALL, value = { "classpath:db-test- script.xml" })
这很有效,但不适用于我之前提到的类。数据集中的数据结构如下所示:
<referralAccount id="2" code="CODE2" />
<referralAccount id="1" code="CODE" parentAccountId="2"/>
但是我在日志中发现的问题如下:
FlatXmlProducer:316 - 表 Account 第 2 行的额外列 (parentAccountId)(全局行号为 5)。这些列将被忽略。 请将额外的列添加到第 1 行,或使用 DTD 确保填充这些列的值,或为 FlatXmlProducer 指定“columnSensing=true”。 有关更多详细信息,请参阅常见问题解答。
问题是,有没有办法通过注释指定 dtd 或 columnSensing?如果可以避免的话,我不想实现 setUp 方法,因为注释似乎是处理此问题的一种干净简单的方法。
谢谢!
最佳答案
如果您使用的是 DBUnit 2.3.0 或更高版本,则可以使用 ColumnSensing 属性,如 dbunit javadoc 中所述。 :
Beware that DbUnit may think a table misses some columns if the first row of that table has one or more null values. You can do one of the following things to avoid this:
Since DBUnit 2.3.0 there is a functionality called "column sensing" which basically reads in the whole XML into a buffer and dynamically adds new columns as they appear. It can be used as demonstrated in the following example:
// since dbunit 2.4.7
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
builder.setInputSource(new File("src/xml/flatXmlTableTest.xml"));
builder.setColumnSensing(true);
IDataSet dataSet = builder.build();
// or dbunit release <= 2.4.6:
boolean enableColumnSensing = true;
IDataSet dataSet = new FlatXmlDataSet(
new File("src/xml/flatXmlTableTest.xml"), false, enableColumnSensing);
因此,如果您使用 spring,您可以创建一个类,例如:
public class ColumnDetectorXmlDataSetLoader extends FlatXmlDataSetLoader {
@Override
protected IDataSet createDataSet(Resource resource) throws Exception {
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
builder.setColumnSensing(true);
InputStream inputStream = resource.getInputStream();
try {
return builder.build(inputStream);
} finally {
inputStream.close();
}
}
}
然后在你的junit测试中:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup("my-dataset.xml")
@DatabaseTearDown("my-dataset.xml")
// Reference to the ColumnDetectorXmlDataSetLoader
@DbUnitConfiguration(dataSetLoader = ColumnDetectorXmlDataSetLoader.class)
public class MyTestTest {
}
在此
关于java - DBUnit,使用带有注释配置的DTD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274682/