0

I have some tests in error after upgrading from Spring boot 2.5.6 to 2.7.3. With this upgrade, h2 version has been upgraded from 1.4.200 to 2.1.214. In the pom, the version of spring-test-dbunit used is 1.3.0. The version of dbunit is 2.7.3.

For information we use Oracle for the database and h2 for tests.

Our application works on two different schema.

I have some entities like this:

@Entity
@Table(name = "TABLEONE")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY) 
@Immutable
public class TableOne implements Serializable {
    ...
}

@Entity
@Table(schema = "OTHERSCHEMA", name = "TABLETWO_C") 
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TableTwo {
    ...
}

@Entity
@Table(schema = "OTHERSCHEMA", name = "TABLETHREE") 
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY) 
@Immutable
public class TableThree {
    ...
    @ManyToOne
    @JoinColumn(name = "ID_TABLEONE")
    private TableOne tableOne;
    ....
}

In the application.yml for tests, we have this (for "spring.datasource.url"):

url: jdbc:h2:mem:my-app;DB_CLOSE_DELAY=-1;MODE=Oracle

For the tests, we have one configuration for dbunit like this:

@TestConfiguration
public class DbUnitTestConfiguration {

    @Autowired
    private DataSource dataSource;

    public DatabaseConfigBean getDatabaseConfigBean(boolean qualifiedTableNames) {
        final DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();
        databaseConfigBean.setDatatypeFactory(new H2DataTypeFactory());
        databaseConfigBean.setQualifiedTableNames(qualifiedTableNames);
        return databaseConfigBean;
    }

    @Bean(name = "dbUnitDatabaseConnection")
    public DatabaseDataSourceConnectionFactoryBean getDatabaseDataSourceConnectionFactoryBean() {
        final DatabaseDataSourceConnectionFactoryBean databaseConfigBean = new DatabaseDataSourceConnectionFactoryBean();
        databaseConfigBean.setDatabaseConfig(getDatabaseConfigBean(false));
        databaseConfigBean.setDataSource(dataSource);
        return databaseConfigBean;
    }

    @Bean(name = "otherSchemaDatabaseConnection")
    public DatabaseDataSourceConnectionFactoryBean getOtherSchemaDatabaseDataSourceConnectionFactoryBean() {
        final DatabaseDataSourceConnectionFactoryBean databaseConfigBean = new DatabaseDataSourceConnectionFactoryBean();
        databaseConfigBean.setDatabaseConfig(getDatabaseConfigBean(true));
        databaseConfigBean.setDataSource(dataSource);
        databaseConfigBean.setSchema("OTHERSCHEMA");
        return databaseConfigBean;
    }
}

The tests are like this:

@ExtendWith(SpringExtension.class)
@SpringBootTest(properties = { "spring.mvc.pathmatch.matching-strategy=ant-path-matcher" })
@AutoConfigureMockMvc
@ContextConfiguration(classes = { SecurityTestConfiguration.class, SpecificWebMvcTestConfiguration.class,
        DbUnitTestConfiguration.class })
@TestExecutionListeners(listeners = DbUnitTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
@DbUnitConfiguration(databaseConnection = { "dbUnitDatabaseConnection", "otherSchemaDatabaseConnection" })
@WithMockUser
@DatabaseSetup(connection = "otherSchemaDatabaseConnection", value = { "classpath:dataSet/tableTwo.xml" })
@DatabaseSetup(value = { "classpath:dataSet/tableOne.xml" })
@DatabaseTearDown(value = { "classpath:dataSet/tableOne.xml" }, type = DatabaseOperation.DELETE_ALL)
@DatabaseTearDown(connection = "otherSchemaDatabaseConnection", value = {
        "classpath:dataSet/tableTwo.xml" }, type = DatabaseOperation.DELETE_ALL)
public class MyTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void myTest() throws Exception {
        ...
    }
}

I have errors like this:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "TABLEONE" not found; SQL statement:
alter table otherschema.tablethree add constraint FKexerkn1ptnbr1k56036qd0snn foreign key (id_tableone) references tableone [42102-214]

and

org.dbunit.dataset.NoSuchTableException: OTHERSCHEMA.TABLETWO_C

I also see that log:

org.dbunit.database.DatabaseDataSet      : Table 'OTHERSCHEMA.TABLETWO_C' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[OTHERSCHEMA.TABLETEN, OTHERSCHEMA.TABLEELEVEN...], _tableMap={OTHERSCHEMA.TABLETEN=null, OTHERSCHEMA.TABLEELEVEN=null...}, _caseSensitiveTableNames=false]

It seems that the configuration that was done before and working with previous version of h2 does not work with new version of h2. I saw a solution by setting qualified table names to true and it is what is done for the other schema so I do not understand.

For the first error, the alter table works on a table of otherschema and did not find tableone that is in the default schema (dbUnitDatabaseConnection connection) so it seems to be a problem between dbUnitDatabaseConnection and otherSchemaDatabaseConnection no?

For the second error, I do not understand why the table does not exist in the list of tables with OTHERSCHEMA indicated in the log.

Can you help me please?

phildeg31
  • 169
  • 1
  • 14

0 Answers0