DbUnit + springtestdbunit 中的错误 table 计数
Wrong table count in DbUnit + springtestdbunit
我有 2 个 table,但是来自此 post 的代码导致异常。
我做错了什么?
如何解决这个问题?
文本失败
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>
POST 更新(信息,被添加的人请求)
测试
@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);
}
empty.xml
<dataset>
<CATEGORY/>
<CATEGORY_RELATIONS/>
</dataset>
没有 table 个计数器
@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);
}
这应该会导致在控制台上打印测试方法有权访问的表列表。
我有 2 个 table,但是来自此 post 的代码导致异常。
我做错了什么?
如何解决这个问题?
文本失败
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>
POST 更新(信息,被添加的人请求)
测试
@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);
}
empty.xml
<dataset>
<CATEGORY/>
<CATEGORY_RELATIONS/>
</dataset>
没有 table 个计数器
@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);
}
这应该会导致在控制台上打印测试方法有权访问的表列表。