Skip to content

Introduce Testcontainers as a means to run integration tests against a wider set of RDBMSs #2256

Closed
@gregturn

Description

@gregturn

Testcontainers affords us the means to test against more database types. Introducing it can increase confidence in supported technologies. Additionally, certain test cases are disabled because the related features are not support on HSQL, the embedded engine currently used.

Related: #409.

Also see: https://github.com/GabrielBB/spring-data-jpa-procedure-tests

For an example of using Java Config to spin up Testcontainers for a given test case, see the following:

@ContextConfiguration
@Transactional
@ExtendWith(SpringExtension.class)
public class ScratchIntegrationTests {

	@PersistenceContext EntityManager em;

      // insert test methods here

	@Configuration
	@EnableJpaRepositories(basePackageClasses = DummyRepository.class,
			includeFilters = { @Filter(pattern = ".*DummyRepository", type = FilterType.REGEX) })
	static class MySqlConfiguration {

		private static PostgreSQLContainer<?> POSTGRESQL_CONTAINER;

		@Bean
		public DataSource dataSource() {

			if (POSTGRESQL_CONTAINER == null) {

				PostgreSQLContainer<?> container = new PostgreSQLContainer<>();
				container.start();

				POSTGRESQL_CONTAINER = container;
			}

			PGSimpleDataSource dataSource = new PGSimpleDataSource();
			dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
			dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());
			dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());

			return dataSource;
		}

		@Bean
		public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
				AbstractJpaVendorAdapter jpaVendorAdapter) {

			LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
			entityManagerFactoryBean.setDataSource(dataSource);
			entityManagerFactoryBean.setPersistenceUnitName("spring-data-jpa");
			entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
			entityManagerFactoryBean.setJpaProperties(this.additionalProperties());

			return entityManagerFactoryBean;
		}

		@Bean
		DataSourceInitializer initializer(DataSource dataSource, Environment environment) {

			DataSourceInitializer initializer = new DataSourceInitializer();
			initializer.setDataSource(dataSource);

			ClassPathResource script = new ClassPathResource("scripts/schema-stored-procedures.sql", getClass().getClassLoader());
			ResourceDatabasePopulator populator = new ResourceDatabasePopulator(script);
			initializer.setDatabasePopulator(populator);

			return initializer;
		}


		@Bean
		public AbstractJpaVendorAdapter jpaVendorAdapter() {

			HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
			jpaVendorAdapter.setGenerateDdl(true);
			jpaVendorAdapter.setDatabase(Database.POSTGRESQL);

			return jpaVendorAdapter;
		}

		@Bean
		public TransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
			return new JpaTransactionManager(entityManagerFactory);
		}

		@Bean
		public SampleEvaluationContextExtension sampleEvaluationContextExtension() {
			return new SampleEvaluationContextExtension();
		}

		@Bean
		public PropertiesFactoryBean jpaProperties() {
			return new PropertiesFactoryBean();
		}

		private Properties additionalProperties() {

			Properties properties = new Properties();
			properties.setProperty("hibernate.dialect", PostgresPlusDialect.class.getCanonicalName());

			return properties;
		}
	}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions