spring-boottestingspring-data-jpatestcontainersmulti-database

SpringBoot, test containers to test queries sent to external DB


My Spring app has its own DB for persistence. The same app needs to send ad-hoc queries to external databases. Queries are provided by users.

Is there any way to utilize spring test containers in order to test this functionaly? My goal is:

I see many examples where App itself is tested against test containers, but can I just start container w/o plugging it as App persistence DB?


Solution

  • can I just start container w/o plugging it as App persistence DB?

    Yes, this is perfectly possible. Testcontainers in itself has nothing to do with Spring or Spring Boot.

    What you would do is:

    Spring Data JDBC does exactly that to run tests against various databases. I add the class doing that for MySQL in the end. It is a Spring application context configuration, but you could put that in a JUnit before method, a JUnit 4 rule or a JUnit 5 extension or just a normal method that you call at the start of your test.

    @Configuration
    @Profile("mysql")
    class MySqlDataSourceConfiguration extends DataSourceConfiguration {
    
        private static final MySQLContainer MYSQL_CONTAINER = new MySQLContainer().withConfigurationOverride("");
    
        static {
            MYSQL_CONTAINER.start();
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.jdbc.testing.DataSourceConfiguration#createDataSource()
         */
        @Override
        protected DataSource createDataSource() {
    
            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUrl(MYSQL_CONTAINER.getJdbcUrl());
            dataSource.setUser(MYSQL_CONTAINER.getUsername());
            dataSource.setPassword(MYSQL_CONTAINER.getPassword());
            dataSource.setDatabaseName(MYSQL_CONTAINER.getDatabaseName());
    
            return dataSource;
        }
    
        @PostConstruct
        public void initDatabase() throws SQLException, ScriptException {
            ScriptUtils.executeSqlScript(createDataSource().getConnection(), null, "DROP DATABASE test;CREATE DATABASE test;");
        }
    }