【问题标题】:Wrong table count in DbUnit + springtestdbunitDbUnit + springtestdbunit 中的表计数错误
【发布时间】:2016-03-21 15:28:48
【问题描述】:

我有 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>

POST UPDATE(信息,由添加的人请求)

测试

@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 {//...

【问题讨论】:

    标签: java sql hibernate unit-testing dbunit


    【解决方案1】:

    数据集 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); }

    这应该会导致测试方法有权访问的表列表打印在控制台上。

    【讨论】:

    • 其他测试进展顺利,但使用了这样的 xml 结构:“”。我只有一个数据库。
    • 好的,你能发布失败的测试用例的代码吗?大概您有一些代码正在计算表的数量,并且您将预期数字(根据输出似乎是 5)和实际数字(根据输出似乎是 2)传递给断言?
    • @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 &lt;dataset&gt; &lt;CATEGORY/&gt; &lt;CATEGORY_RELATIONS/&gt; &lt;/dataset&gt;
    • 无表计数器@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 {
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多