Best Testcontainers-java code snippet using org.testcontainers.jdbc.mysql.JDBCDriverWithPoolTest.getHikariDataSource
Source:JDBCDriverWithPoolTest.java  
...32    public static Iterable<Supplier<DataSource>> dataSourceSuppliers() {33        return asList(34            JDBCDriverWithPoolTest::getTomcatDataSourceWithDriverClassName,35            JDBCDriverWithPoolTest::getTomcatDataSource,36            JDBCDriverWithPoolTest::getHikariDataSourceWithDriverClassName,37            JDBCDriverWithPoolTest::getHikariDataSource,38            JDBCDriverWithPoolTest::getViburDataSourceWithDriverClassName,39            JDBCDriverWithPoolTest::getViburDataSource40        );41    }42    public JDBCDriverWithPoolTest(Supplier<DataSource> dataSourceSupplier) {43        this.dataSource = dataSourceSupplier.get();44    }45    private ExecutorService executorService = Executors.newFixedThreadPool(5);46    @Test47    public void testMySQLWithConnectionPoolUsingSameContainer() throws SQLException, InterruptedException {48        // Populate the database with some data in multiple threads, so that multiple connections from the pool will be used49        for (int i = 0; i < 100; i++) {50            executorService.submit(() -> {51                try {52                    new QueryRunner(dataSource).insert("INSERT INTO my_counter (n) VALUES (5)",53                        (ResultSetHandler<Object>) rs -> true);54                } catch (SQLException e) {55                    e.printStackTrace();56                }57            });58        }59        // Complete population of the database60        executorService.shutdown();61        executorService.awaitTermination(5, TimeUnit.MINUTES);62        // compare to expected results63        int count = new QueryRunner(dataSource).query("SELECT COUNT(1) FROM my_counter", rs -> {64            rs.next();65            return rs.getInt(1);66        });67        assertEquals("Reuse of a datasource points to the same DB container", 100, count);68        int sum = new QueryRunner(dataSource).query("SELECT SUM(n) FROM my_counter", rs -> {69            rs.next();70            return rs.getInt(1);71        });72        // 100 records * 5 = 500 expected73        assertEquals("Reuse of a datasource points to the same DB container", 500, sum);74    }75    private static DataSource getTomcatDataSourceWithDriverClassName() {76        PoolProperties poolProperties = new PoolProperties();77        poolProperties.setUrl(URL + ";TEST=TOMCAT_WITH_CLASSNAME"); // append a dummy URL element to ensure different DB per test78        poolProperties.setValidationQuery("SELECT 1");79        poolProperties.setMinIdle(3);80        poolProperties.setMaxActive(10);81        poolProperties.setDriverClassName(ContainerDatabaseDriver.class.getName());82        return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);83    }84    private static DataSource getTomcatDataSource() {85        PoolProperties poolProperties = new PoolProperties();86        poolProperties.setUrl(URL + ";TEST=TOMCAT"); // append a dummy URL element to ensure different DB per test87        poolProperties.setValidationQuery("SELECT 1");88        poolProperties.setInitialSize(3);89        poolProperties.setMaxActive(10);90        return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);91    }92    private static HikariDataSource getHikariDataSourceWithDriverClassName() {93        HikariConfig hikariConfig = new HikariConfig();94        hikariConfig.setJdbcUrl(URL + ";TEST=HIKARI_WITH_CLASSNAME"); // append a dummy URL element to ensure different DB per test95        hikariConfig.setConnectionTestQuery("SELECT 1");96        hikariConfig.setMinimumIdle(3);97        hikariConfig.setMaximumPoolSize(10);98        hikariConfig.setDriverClassName(ContainerDatabaseDriver.class.getName());99        return new HikariDataSource(hikariConfig);100    }101    private static HikariDataSource getHikariDataSource() {102        HikariConfig hikariConfig = new HikariConfig();103        hikariConfig.setJdbcUrl(URL + ";TEST=HIKARI"); // append a dummy URL element to ensure different DB per test104        hikariConfig.setConnectionTestQuery("SELECT 1");105        hikariConfig.setMinimumIdle(3);106        hikariConfig.setMaximumPoolSize(10);107        return new HikariDataSource(hikariConfig);108    }109    private static DataSource getViburDataSourceWithDriverClassName() {110        ViburDBCPDataSource ds = new ViburDBCPDataSource();111        ds.setJdbcUrl(URL + ";TEST=VIBUR_WITH_CLASSNAME");112        ds.setUsername("any");  // Recent versions of Vibur require a username, even though it will not be used113        ds.setPassword("");114        ds.setPoolInitialSize(3);115        ds.setPoolMaxSize(10);...getHikariDataSource
Using AI Code Generation
1import org.testcontainers.jdbc.ContainerDatabaseDriver;2import org.testcontainers.jdbc.JdbcDatabaseDelegate;3import org.testcontainers.jdbc.JdbcDriverTestUtils;4import org.testcontainers.jdbc.JdbcDriverUrl;5import org.testcontainers.jdbc.MySQLDriverDelegate;6import org.testcontainers.jdbc.PoolingDatabaseDelegate;7import java.sql.Connection;8import java.sql.DriverManager;9import java.sql.PreparedStatement;10import java.sql.ResultSet;11import java.sql.SQLException;12import java.util.Properties;13import java.util.UUID;14import javax.sql.DataSource;15import static org.junit.Assert.assertEquals;16import static org.junit.Assert.assertTrue;17public class JDBCDriverWithPoolTest {18    private static final String DRIVER_CLASS_NAME = "org.testcontainers.jdbc.ContainerDatabaseDriver";19    static {20        try {21            DriverManager.registerDriver(new ContainerDatabaseDriver());22        } catch (SQLException e) {23            throw new RuntimeException("Could not register driver: " + e.getMessage(), e);24        }25    }26    public void testGetHikariDataSource() throws SQLException {27        DataSource dataSource = getHikariDataSource();28        try (Connection connection = dataSource.getConnection()) {29            try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT 1")) {30                try (ResultSet resultSet = preparedStatement.executeQuery()) {31                    assertTrue(resultSet.next());32                    assertEquals(1, resultSet.getInt(1));33                }34            }35        }36    }37    public static DataSource getHikariDataSource() throws SQLException {38        return getHikariDataSource(URL_PREFIX);39    }40    public static DataSource getHikariDataSource(String url) throws SQLException {41        return getHikariDataSource(url, new Properties());42    }43    public static DataSource getHikariDataSource(String url, Properties properties) throws SQLException {44        JdbcDatabaseDelegate delegate = new PoolingDatabaseDelegate(new MySQLDriverDelegate());45        JdbcDriverUrl jdbcDriverUrl = new JdbcDriverUrl(url, properties);46        return delegate.getDataSource(jdbcDriverUrl);47    }48}getHikariDataSource
Using AI Code Generation
1import com.zaxxer.hikari.HikariDataSource2import org.testcontainers.jdbc.ContainerDatabaseDriver3import org.testcontainers.jdbc.ConnectionUrl4import org.testcontainers.jdbc.ConnectionUrlBuilder5import org.testcontainers.jdbc.ConnectionUrlBuilderTest6import org.testcontainers.jdbc.JdbcDatabaseDelegate7import org.testcontainers.jdbc.JdbcDriverTest8import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDataSource9import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriver10import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverTest11import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest12import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.TestContainerDataSourceWithPool13import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.TestContainerDataSourceWithPoolTest14import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getHikariDataSource15import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getHikariDataSourceTest16import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDataSourceWithPool17import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDataSourceWithPoolTest18import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPool19import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPoolTest20import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPoolTestWithDriver21import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPoolTestWithDriverTest22import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPoolTestWithDriverTestWithPool23import org.testcontainers.jdbc.JdbcDriverTest.TestContainerDriverWithPoolTest.getTestContainerDriverWithPoolTestWithDriverTestWithPoolTest24import org.testcontainers.jdbc.JdbcDriverTest.getTestContainerDataSource25import org.testcontainers.jdbc.JdbcDriverTest.getTestContainerDataSourceTest26import org.testcontainers.jdbc.JdbcDriverTest.getTestContainerDriver27import org.testcontainers.jdbc.JdbcDriverTest.getTestContainerDriverTest28import org.testcontainers.jdbc.JdbcDriverTest.getTestContainerDriverTestWithDriver29import org.testLearn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
