我有 2 个表,但这篇文章中的代码导致异常。
我做错了什么?
如何解决这个问题?
失败文本
junit.framework.ComparisonFailure: table count
Expected :5
Actual :2
Pom依赖
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.2.1</version>
</dependency>
数据集
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<table name="CATEGORY">
<column>CATEGORY_ID</column>
<row>
<value>1</value>
</row>
<row>
<value>2</value>
</row>
</table>
<table name="CATEGORY_RELATIONS">
<column>CATEGORY_RELATIONS_PARENT_ID</column>
<column>CATEGORY_RELATIONS_CATEGORY_ID</column>
<column>ID</column>
<row>
<value>1</value>
<value>2</value>
<null/>
</row>
</table>
</dataset>
发布更新(信息,由添加的人请求)
测试
@Test
@DatabaseSetup("classpath:data-sets/empty.xml")
@ExpectedDatabase("classpath:data-sets/categories/save.xml")
public void save() throws Exception {
testTarget.save(parentCategory);
testTarget.save(childCategory);
}
空.xml
<dataset>
<CATEGORY/>
<CATEGORY_RELATIONS/>
</dataset>
没有表计数器
@ActiveProfiles("test") @RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class,
HSqlTestExecutionListener.class})
@ContextConfiguration({"classpath:contexts/bean-locations.xml"})
public class SpringHsqlTest {//...
最佳答案
数据集 XML 清楚地显示了正在创建的两个表,大概是在单独的单元测试数据库中。
您的测试代码似乎需要 2 个表,但似乎存在 5 个表 - 让我们看看是否可以确认这一点。
我假设您的 save() 测试方法中的 testTarget 对象是某种 Spring 存储库。因此,您应该能够将以下内容添加到该存储库:
@Query(value = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='TABLE' ", nativeQuery = true)
List<String> tableNames();
然后在测试中的save()方法中:
for (String table : testTarget.tableNames()) {
System.out.println(table);
}
这应该会导致测试方法有权访问的表列表打印在控制台上。
关于java - DbUnit + springtestdbunit 中的表计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135201/