我有两个表,但是这篇文章中的代码会导致异常。
我做错什么了?
怎么解决这个问题?
失败文本
junit.framework.ComparisonFailure: table count
Expected :5
Actual :2Pom依赖关系
<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);
} empty.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 {//...发布于 2016-03-21 15:38:36
dataset XML清楚地显示了正在创建的两个表,大概在一个独立的单元测试数据库中。
您的测试代码似乎需要2个表,但似乎存在5个表--让我们看看是否可以确认这一点。
我假定testTarget对象在您的save()测试方法中是某种类型的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); }
这将导致将测试方法可以访问的表列表打印到控制台上。
https://stackoverflow.com/questions/36135201
复制相似问题