it's not working DBUnit @DatabaseSetup with CSV loader when included period table name

645 Views Asked by At

when I use @DatabaseSetup in the test. there is an error "no such table" in the title case.

it's working collect below case.


CSV loader

public class CsvDataSetLoader extends AbstractDataSetLoader {
    @Override
    protected IDataSet createDataSet(Resource resource) throws Exception {
        return new CsvDataSet(resource.getFile());
    }
}

sql:

create table test
(
    id int
);

table-ordering.txt

test

test.csv

id
1
2
3

test class

@SpringBootTest(classes = {TestConfig.class})
@RunWith(SpringRunner.class)
@DbUnitConfiguration(dataSetLoader = CsvDataSetLoader.class) 
@TestExecutionListeners({
  DependencyInjectionTestExecutionListener.class,
  DbUnitTestExecutionListener.class
})
@Transactional
public class JsonDatabaseTest {

  @Autowired
  JdbcTemplate jdbcTemplate;

  @Test
  @DatabaseSetup("/table/init")
  public void test(){
    System.out.println(jdbcTemplate.queryForList("select * from test"));
    // printed out 1,2,3
  }
}


but it's happened error 'no such table sh.test'

sql:

create schema sh;

create table sh.test
(
    id int
);

table-ordering.txt

sh.test

sh.test.csv

id
1
2
3

test class

@SpringBootTest(classes = {TestConfig.class})
@RunWith(SpringRunner.class)
@DbUnitConfiguration(dataSetLoader = CsvDataSetLoader.class) 
@TestExecutionListeners({
  DependencyInjectionTestExecutionListener.class,
  DbUnitTestExecutionListener.class
})
@Transactional
public class JsonDatabaseTest {

  @Autowired
  JdbcTemplate jdbcTemplate;

  @Test
  @DatabaseSetup("/table/init") // if comment out this, no error occurs
  public void test(){
    System.out.println(jdbcTemplate.queryForList("select * from sh.test"));
  }
}

When I comment out the code, no error occurs, so the table named "sh.test" must indeed exist. but why is @DatabaseSetup missing the table? I can't guess.....

1

There are 1 best solutions below

0
Minori Akizuki On

it was simply.

at setting class

  @Bean
  H2Connection h2Connection() throws DatabaseUnitException, SQLException{
    return new H2Connection(dataSource().getConnection(), "sh");
  }

at test class

@DbUnitConfiguration(databaseConnection = "h2Connection", dataSetLoader = CsvDataSetLoader.class) 

Now I don't need to add the schema name to file names, etc.