sql-serverjunitdbunit

How can I specify a schema name for DbUnit to do the insert into?


I'm using DBUnit to insert data (dumped from a Postgres DB) into SQL Server, but want to do the insert into schema "rules", not the default "dbo" schema:

Class.forName(net.sourceforge.jtds.jdbc.Driver.class.getName());
Connection sqlsCon = DriverManager.getConnection("jdbc:jtds:sqlserver://5.5.5.5:7000;databaseName=THE_DB", "THE_USER", "THE_PW");
IDatabaseConnection sqlsDbCon = new DatabaseConnection(sqlsCon);
DatabaseOperation.CLEAN_INSERT.execute(sqlsDbCon, partialDataSet);

Thank you!


Solution

  • If you use spring boot 2 then you can provide a custom configuration in your config:

    @TestConfiguration
    public class MyDbUnitConfiguration {
    
      @Bean
      public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(DataSource dataSource) {
            DatabaseConfigBean databaseConfig = new DatabaseConfigBean();
            databaseConfig.setQualifiedTableNames(Boolean.TRUE);
    
            DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactory =
            new DatabaseDataSourceConnectionFactoryBean();
            databaseDataSourceConnectionFactory.setDatabaseConfig(databaseConfig);
            databaseDataSourceConnectionFactory.setDataSource(dataSource);
            return databaseDataSourceConnectionFactory;
      }
    }
    

    Include this configuration and update your datasets.xml to fully qualified names.