Justin.Bell Justin.Bell - 2 months ago 21
Java Question

H2 Integration Test Database not following the same naming as MySQL prod DB

I've been working on writing some integration tests that use an in memory (H2) database, but I'm stuck on this error.


Caused by: org.h2.jdbc.JdbcSQLException: Table "TESTTABLE" not found; SQL statement:


The issue is, that in production Hibernate is changing both table names and column names from camel casing to underscores. (TestTable -> test_table) but with the H2 test DB it is just going to uppercase (TestTable -> TESTTABLE)

Here is my test config class:

@Configuration
@EnableJpaRepositories(basePackageClasses = {
TestTableRepository.class
})
public class InMemoryDataConfig {

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addDefaultScripts()
.build();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"org.test.domain.entity"});

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(buildHibernateProperties());

return em;
}

protected Properties buildHibernateProperties() {
Properties hibernateProperties = new Properties();

hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "true");
hibernateProperties.setProperty("hibernate.use_sql_comments", "true");
hibernateProperties.setProperty("hibernate.format_sql", "true");

return hibernateProperties;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}

@Bean
public TestService testService() {
return new TestService();
}


}

I would rather not have to annotate everything just so the tests can know what the names are.

Any thoughts or suggestions on fixing this? Thanks for any help!

Answer

This doesn't quite answer the specific question I asked, but I was able to get it working in a much more simple way. If you scrap everything I had posted in my original question you just need to do a couple things.

First you must include the dependency. I'm using gradle so I just added this:

testCompile("com.h2database:h2:1.4.191")

Next, add these annotations to your test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MultifamilyIntegrationTest {

    @Autowired
    protected TestService testService;

    @Test
    public void test() {
        testService.getUsers();
    }

}

Last, you must include a data.sql and a schema.sql file inside your /src/test/resources folder.

The schema.sql file is a creation script for everything schema related.

The data.sql file is to insert all of the needed data.