Wrong table count in DbUnit + springtestdbunit

I have 2 tables, but code from this post leads exception.

What I am doing wrong?

How fix this?

Fail text

junit.framework.ComparisonFailure: table count
Expected :5
Actual   :2

Pom dependencies

<dependency>
    <groupId>com.github.springtestdbunit</groupId>
    <artifactId>spring-test-dbunit</artifactId>
    <version>1.2.1</version>
</dependency>

Dataset

<?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 (information, requested by people added)

Test

@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> 

No table counters

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

Answers


The dataset XML clearly shows two tables being created, presumably in a separate unit test database.

Your test code seems to be expecting 2 tables, but it seems that 5 exist - let's see if we can confirm that.

I presume the testTarget object in your save() test method is a Spring repository of some kind. So you should be able to add the following to that repository:

@Query(value = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='TABLE' ", nativeQuery = true) List<String> tableNames();

then in the save() method in the test:

for (String table : testTarget.tableNames()) { System.out.println(table); }

this should cause the list of tables the test method has access to to be printed on the console.


Need Your Help

order mysql query result in decreasing order

mysql sql select join sql-order-by

I am trying to create a table for marks and rank team in decreasing order.