How to use SimpleMySQLTest class of org.testcontainers.junit.mysql package

Best Testcontainers-java code snippet using org.testcontainers.junit.mysql.SimpleMySQLTest

Source:SimpleMySQLTest.java Github

copy

Full Screen

...14 *15 *16 * @author richardnorth17 */18public class SimpleMySQLTest {19 private static final Logger logger = LoggerFactory.getLogger(SimpleMySQLTest.class);20 /* Ordinarily you wouldn't try and run multiple containers simultaneously - this is just used for testing.21 To avoid memory issues with the default, low memory, docker machine setup, we instantiate only one container22 at a time, inside the test methods themselves.23 */24 /* @ClassRule25 public static MySQLContainer mysql = new MySQLContainer();26 @ClassRule27 public static MySQLContainer mysqlOldVersion = new MySQLContainer("mysql:5.5");28 @ClassRule29 public static MySQLContainer mysqlCustomConfig = new MySQLContainer("mysql:5.6")30 .withConfigurationOverride("somepath/mysql_conf_override");31 */32 @Test33 public void testSimple() throws SQLException {34 MySQLContainer mysql = ((MySQLContainer) (new MySQLContainer().withConfigurationOverride("somepath/mysql_conf_override").withLogConsumer(new Slf4jLogConsumer(SimpleMySQLTest.logger))));35 mysql.start();36 try {37 ResultSet resultSet = performQuery(mysql, "SELECT 1");38 int resultSetInt = resultSet.getInt(1);39 assertEquals("A basic SELECT query succeeds", 1, resultSetInt);40 } finally {41 mysql.stop();42 }43 }44 @Test45 public void testSpecificVersion() throws SQLException {46 MySQLContainer mysqlOldVersion = ((MySQLContainer) (new MySQLContainer("mysql:5.5").withConfigurationOverride("somepath/mysql_conf_override").withLogConsumer(new Slf4jLogConsumer(SimpleMySQLTest.logger))));47 mysqlOldVersion.start();48 try {49 ResultSet resultSet = performQuery(mysqlOldVersion, "SELECT VERSION()");50 String resultSetString = resultSet.getString(1);51 assertTrue("The database version can be set using a container rule parameter", resultSetString.startsWith("5.5"));52 } finally {53 mysqlOldVersion.stop();54 }55 }56 @Test57 public void testMySQLWithCustomIniFile() throws SQLException {58 Assume.assumeFalse(IS_OS_WINDOWS);59 MySQLContainer mysqlCustomConfig = new MySQLContainer("mysql:5.6").withConfigurationOverride("somepath/mysql_conf_override");60 mysqlCustomConfig.start();61 try {62 ResultSet resultSet = performQuery(mysqlCustomConfig, "SELECT @@GLOBAL.innodb_file_format");63 String result = resultSet.getString(1);64 assertEquals("The InnoDB file format has been set by the ini file content", "Barracuda", result);65 } finally {66 mysqlCustomConfig.stop();67 }68 }69 @Test70 public void testCommandOverride() throws SQLException {71 MySQLContainer mysqlCustomConfig = ((MySQLContainer) (new MySQLContainer().withCommand("mysqld --auto_increment_increment=42")));72 mysqlCustomConfig.start();73 try {74 ResultSet resultSet = performQuery(mysqlCustomConfig, "show variables like 'auto_increment_increment'");75 String result = resultSet.getString("Value");76 assertEquals("Auto increment increment should be overriden by command line", "42", result);77 } finally {78 mysqlCustomConfig.stop();79 }80 }81 @Test82 public void testMySQL8() throws SQLException {83 Assume.assumeFalse(IS_OS_WINDOWS);84 MySQLContainer container = new MySQLContainer("mysql:8.0.11").withCommand("mysqld --default-authentication-plugin=mysql_native_password");85 container.start();86 try {87 ResultSet resultSet = performQuery(container, "SELECT VERSION()");88 String resultSetString = resultSet.getString(1);89 assertTrue("The database version can be set using a container rule parameter", "8.0.11".equals(resultSetString));90 } finally {91 container.stop();92 }93 }94 @Test95 public void testExplicitInitScript() throws SQLException {96 try (MySQLContainer container = ((MySQLContainer) (new MySQLContainer().withInitScript("somepath/init_mysql.sql").withLogConsumer(new Slf4jLogConsumer(SimpleMySQLTest.logger))))) {97 container.start();98 ResultSet resultSet = performQuery(container, "SELECT foo FROM bar");99 String firstColumnValue = resultSet.getString(1);100 assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);101 }102 }103 @Test104 public void testEmptyPasswordWithNonRootUser() {105 MySQLContainer container = ((MySQLContainer) (new MySQLContainer("mysql:5.5").withDatabaseName("TEST").withUsername("test").withPassword("").withEnv("MYSQL_ROOT_HOST", "%")));106 try {107 container.start();108 Assert.fail("ContainerLaunchException expected to be thrown");109 } catch (ContainerLaunchException e) {110 } finally {...

Full Screen

Full Screen

SimpleMySQLTest

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.MySQLContainer2import org.testcontainers.junit.jupiter.Container3import org.testcontainers.junit.jupiter.Testcontainers4import org.testcontainers.utility.DockerImageName5import org.junit.jupiter.api.Test6import org.junit.jupiter.api.Assertions7import java.sql.Connection8import java.sql.DriverManager9class SimpleMySQLTest {10 companion object {11 val mySQLContainer = MySQLContainer<Nothing>(DockerImageName.parse("mysql:5.7.20"))12 }13 fun testMySQL() {14 Class.forName("com.mysql.jdbc.Driver")15 val connection: Connection = DriverManager.getConnection(mySQLContainer.jdbcUrl, mySQLContainer.username, mySQLContainer.password)16 val statement = connection.createStatement()17 val resultSet = statement.executeQuery("SELECT 1")18 Assertions.assertTrue(resultSet.next())19 Assertions.assertEquals(1, resultSet.getInt(1))20 }21}22import org.testcontainers.containers.MySQLContainer23import org.testcontainers.junit.jupiter.Container24import org.testcontainers.junit.jupiter.Testcontainers25import org.testcontainers.utility.DockerImageName26import org.junit.jupiter.api.Test27import org.junit.jupiter.api.Assertions28import java.sql.Connection29import java.sql.DriverManager30class SimpleMySQLTest {31 companion object {32 val mySQLContainer = MySQLContainer<Nothing>(DockerImageName.parse("mysql:5.7.20"))33 }34 fun testMySQL() {35 Class.forName("com.mysql.jdbc.Driver")36 val connection: Connection = DriverManager.getConnection(mySQLContainer.jdbcUrl, mySQLContainer.username, mySQLContainer.password)37 val statement = connection.createStatement()38 val resultSet = statement.executeQuery("SELECT 1")39 Assertions.assertTrue(resultSet.next())40 Assertions.assertEquals(1, resultSet.getInt(1))41 }42}

Full Screen

Full Screen

SimpleMySQLTest

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.testcontainers.containers.MySQLContainer;3import org.testcontainers.jdbc.ContainerDatabaseDriver;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6import java.sql.*;7import static org.junit.jupiter.api.Assertions.assertEquals;8public class SimpleMySQLTest {9 private static final MySQLContainer<?> mysql57Container = new MySQLContainer<>("mysql:5.7.22")10 .withDatabaseName("test")11 .withUsername("test")12 .withPassword("test");13 private static final MySQLContainer<?> mysql80Container = new MySQLContainer<>("mysql:8.0.11")14 .withDatabaseName("test")15 .withUsername("test")16 .withPassword("test");17 public void testMySQL57() throws SQLException {18 registerContainerDriver(mysql57Container);19 Connection connection = DriverManager.getConnection(mysql57Container.getJdbcUrl(), mysql57Container.getUsername(), mysql57Container.getPassword());20 Statement statement = connection.createStatement();21 statement.execute("CREATE TABLE test_table (id INT, name VARCHAR(255))");22 statement.execute("INSERT INTO test_table VALUES (1, 'test1')");23 statement.execute("INSERT INTO test_table VALUES (2, 'test2')");24 ResultSet resultSet = statement.executeQuery("SELECT id, name FROM test_table");25 resultSet.next();26 assertEquals(1, resultSet.getInt(1));27 assertEquals("test1", resultSet.getString(2));28 resultSet.next();29 assertEquals(2, resultSet.getInt(1));30 assertEquals("test2", resultSet.getString(2));31 statement.execute("DROP TABLE test_table");32 resultSet.close();33 statement.close();34 connection.close();35 }36 public void testMySQL80() throws SQLException {37 registerContainerDriver(mysql80Container);

Full Screen

Full Screen

SimpleMySQLTest

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.MySQLContainer;3import org.testcontainers.junit.jupiter.Container;4import org.testcontainers.junit.jupiter.Testcontainers;5class SimpleMySQLTest {6 static MySQLContainer mysql = new MySQLContainer();7 void test() {8 System.out.println("mysql.getJdbcUrl() = " + mysql.getJdbcUrl());9 System.out.println("mysql.getUsername() = " + mysql.getUsername());10 System.out.println("mysql.getPassword() = " + mysql.getPassword());11 }12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn 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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful