This is an example Spring data jpa project using integration testing with test containers Oracle
This is an example how to use integration test in Spring data jpa project using test container with Oracle database. If your test couldn’t be tested using in memory database approach because you are using some Oracle custom sql definition you could use test container to be able to create a real Oracle database that will be start/stop automatically inside your test class.
-
Add 3 test container dependencies in your pom.xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.Testcontainers</groupId>
<artifactId>Testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>12.2.0.1</scope>
</dependency>
-
Define your custom Oracle test profile OracleTestProfileJPAConfig.kt :
@TestConfiguration
class OracleTestProfileJPAConfig {
@Autowired
private val env: Environment? = null
@Bean
fun dataSource(): DataSource {
container.start()
val dataSource = HikariDataSource()
dataSource.jdbcUrl = container.jdbcUrl
dataSource.username = container.username
dataSource.password = container.password
return dataSource
}
@Bean
fun entityManagerFactory(): LocalContainerEntityManagerFactoryBean {
val em = LocalContainerEntityManagerFactoryBean()
em.dataSource = dataSource()
em.setPackagesToScan(*arrayOf("com.example.kotlinmvc"))
em.jpaVendorAdapter = HibernateJpaVendorAdapter()
em.setJpaProperties(additionalProperties())
return em
}
@Bean
fun transactionManager(entityManagerFactory: EntityManagerFactory?): JpaTransactionManager {
val transactionManager = JpaTransactionManager()
transactionManager.entityManagerFactory = entityManagerFactory
return transactionManager
}
fun additionalProperties(): Properties {
val hibernateProperties = Properties()
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env!!.getProperty("hibernate.hbm2ddl.auto"))
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"))
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"))
return hibernateProperties
}
companion object {
@Container
var container: OracleContainer = OracleContainer("gvenzl/oracle-xe")
.withReuse(true)
}
}
-
Add configuration properties to you application.properties inside test directory (used in OracleTestProfileJPAConfig ) application.properties
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
-
Create a test class using OracleTestProfileJpaConfig: this class will create docker container using Oracle Database MessageServiceImplTest.kt
@Testcontainers
@Transactional
@SpringBootTest(classes = [KotlinWebApplication::class, OracleTestProfileJPAConfig::class])