diff --git a/modules/jdbc/build.gradle b/modules/jdbc/build.gradle index 30d2cb539c4..f1deb59c41d 100644 --- a/modules/jdbc/build.gradle +++ b/modules/jdbc/build.gradle @@ -3,6 +3,9 @@ description = "Testcontainers :: JDBC" dependencies { api project(':testcontainers-database-commons') + testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.0' + + testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4' compileOnly 'org.jetbrains:annotations:26.0.2' testImplementation 'commons-dbutils:commons-dbutils:1.8.1' testImplementation 'org.vibur:vibur-dbcp:26.0' @@ -13,3 +16,7 @@ dependencies { exclude(module: 'hamcrest-core') } } + +test { + useJUnitPlatform() +} diff --git a/modules/jdbc/src/test/java/org/testcontainers/containers/JdbcDatabaseContainerTest.java b/modules/jdbc/src/test/java/org/testcontainers/containers/JdbcDatabaseContainerTest.java index 1dc6646bd17..ca41c3f5d1c 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/containers/JdbcDatabaseContainerTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/containers/JdbcDatabaseContainerTest.java @@ -1,7 +1,7 @@ package org.testcontainers.containers; import lombok.NonNull; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import java.sql.Connection; @@ -10,10 +10,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; -public class JdbcDatabaseContainerTest { +class JdbcDatabaseContainerTest { @Test - public void anExceptionIsThrownIfJdbcIsNotAvailable() { + void anExceptionIsThrownIfJdbcIsNotAvailable() { JdbcDatabaseContainer jdbcContainer = new JdbcDatabaseContainerStub("mysql:latest") .withStartupTimeoutSeconds(1); diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlDriversTests.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlDriversTests.java index 394451bc6a9..5797ee0083c 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlDriversTests.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlDriversTests.java @@ -1,127 +1,126 @@ package org.testcontainers.jdbc; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Arrays; import java.util.Optional; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; /** * This Test class validates that all supported JDBC URL's can be parsed by ConnectionUrl class. */ -@RunWith(Parameterized.class) -public class ConnectionUrlDriversTests { +class ConnectionUrlDriversTests { - @Parameter - public String jdbcUrl; - - @Parameter(1) - public String databaseType; - - @Parameter(2) - public Optional tag; - - @Parameter(3) - public String dbHostString; - - @Parameter(4) - public String databaseName; - - @Parameterized.Parameters(name = "{index} - {0}") - public static Iterable data() { - return Arrays.asList( - new Object[][] { - { "jdbc:tc:mysql:8.0.36://hostname/test", "mysql", Optional.of("8.0.36"), "hostname/test", "test" }, - { "jdbc:tc:mysql://hostname/test", "mysql", Optional.empty(), "hostname/test", "test" }, - { - "jdbc:tc:postgresql:1.2.3://hostname/test", - "postgresql", - Optional.of("1.2.3"), - "hostname/test", - "test", - }, - { "jdbc:tc:postgresql://hostname/test", "postgresql", Optional.empty(), "hostname/test", "test" }, - { - "jdbc:tc:sqlserver:1.2.3://localhost;instance=SQLEXPRESS:1433;databaseName=test", - "sqlserver", - Optional.of("1.2.3"), - "localhost;instance=SQLEXPRESS:1433;databaseName=test", - "test", - }, - { - "jdbc:tc:sqlserver://localhost;instance=SQLEXPRESS:1433;databaseName=test", - "sqlserver", - Optional.empty(), - "localhost;instance=SQLEXPRESS:1433;databaseName=test", - "test", - }, - { - "jdbc:tc:mariadb:1.2.3://localhost:3306/test", - "mariadb", - Optional.of("1.2.3"), - "localhost:3306/test", - "test", - }, - { "jdbc:tc:mariadb://localhost:3306/test", "mariadb", Optional.empty(), "localhost:3306/test", "test" }, - { - "jdbc:tc:oracle:1.2.3:thin://@localhost:1521/test", - "oracle", - Optional.of("1.2.3"), - "localhost:1521/test", - "test", - }, - { - "jdbc:tc:oracle:1.2.3:thin:@localhost:1521/test", - "oracle", - Optional.of("1.2.3"), - "localhost:1521/test", - "test", - }, - { - "jdbc:tc:oracle:thin:@localhost:1521/test", - "oracle", - Optional.empty(), - "localhost:1521/test", - "test", - }, - { - "jdbc:tc:oracle:1.2.3:thin:@localhost:1521:test", - "oracle", - Optional.of("1.2.3"), - "localhost:1521:test", - "test", - }, - { - "jdbc:tc:oracle:1.2.3:thin://@localhost:1521:test", - "oracle", - Optional.of("1.2.3"), - "localhost:1521:test", - "test", - }, - { - "jdbc:tc:oracle:1.2.3-anything:thin://@localhost:1521:test", - "oracle", - Optional.of("1.2.3-anything"), - "localhost:1521:test", - "test", - }, - { - "jdbc:tc:oracle:thin:@localhost:1521:test", - "oracle", - Optional.empty(), - "localhost:1521:test", - "test", - }, - } + public static Stream data() { + return Stream.of( + Arguments.arguments( + "jdbc:tc:mysql:8.0.36://hostname/test", + "mysql", + Optional.of("8.0.36"), + "hostname/test", + "test" + ), + Arguments.arguments("jdbc:tc:mysql://hostname/test", "mysql", Optional.empty(), "hostname/test", "test"), + Arguments.arguments( + "jdbc:tc:postgresql:1.2.3://hostname/test", + "postgresql", + Optional.of("1.2.3"), + "hostname/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:postgresql://hostname/test", + "postgresql", + Optional.empty(), + "hostname/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:sqlserver:1.2.3://localhost;instance=SQLEXPRESS:1433;databaseName=test", + "sqlserver", + Optional.of("1.2.3"), + "localhost;instance=SQLEXPRESS:1433;databaseName=test", + "test" + ), + Arguments.arguments( + "jdbc:tc:sqlserver://localhost;instance=SQLEXPRESS:1433;databaseName=test", + "sqlserver", + Optional.empty(), + "localhost;instance=SQLEXPRESS:1433;databaseName=test", + "test" + ), + Arguments.arguments( + "jdbc:tc:mariadb:1.2.3://localhost:3306/test", + "mariadb", + Optional.of("1.2.3"), + "localhost:3306/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:mariadb://localhost:3306/test", + "mariadb", + Optional.empty(), + "localhost:3306/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:1.2.3:thin://@localhost:1521/test", + "oracle", + Optional.of("1.2.3"), + "localhost:1521/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:1.2.3:thin:@localhost:1521/test", + "oracle", + Optional.of("1.2.3"), + "localhost:1521/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:thin:@localhost:1521/test", + "oracle", + Optional.empty(), + "localhost:1521/test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:1.2.3:thin:@localhost:1521:test", + "oracle", + Optional.of("1.2.3"), + "localhost:1521:test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:1.2.3:thin://@localhost:1521:test", + "oracle", + Optional.of("1.2.3"), + "localhost:1521:test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:1.2.3-anything:thin://@localhost:1521:test", + "oracle", + Optional.of("1.2.3-anything"), + "localhost:1521:test", + "test" + ), + Arguments.arguments( + "jdbc:tc:oracle:thin:@localhost:1521:test", + "oracle", + Optional.empty(), + "localhost:1521:test", + "test" + ) ); } - @Test - public void test() { + @ParameterizedTest(name = "{index} - {0}") + @MethodSource("data") + void test(String jdbcUrl, String databaseType, Optional tag, String dbHostString, String databaseName) { ConnectionUrl url = ConnectionUrl.newInstance(jdbcUrl); assertThat(url.getDatabaseType()).as("Database Type is as expected").isEqualTo(databaseType); assertThat(url.getImageTag()).as("Image tag is as expected").isEqualTo(tag); diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java index b6de810e06c..16abff74ec9 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ConnectionUrlTest.java @@ -1,18 +1,14 @@ package org.testcontainers.jdbc; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; -public class ConnectionUrlTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); +class ConnectionUrlTest { @Test - public void testConnectionUrl1() { + void testConnectionUrl1() { String urlString = "jdbc:tc:mysql:8.0.36://somehostname:3306/databasename?a=b&c=d"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -31,7 +27,7 @@ public void testConnectionUrl1() { } @Test - public void testConnectionUrl2() { + void testConnectionUrl2() { String urlString = "jdbc:tc:mysql://somehostname/databasename"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -49,13 +45,13 @@ public void testConnectionUrl2() { } @Test - public void testEmptyQueryParameter() { + void testEmptyQueryParameter() { ConnectionUrl url = ConnectionUrl.newInstance("jdbc:tc:mysql://somehostname/databasename?key="); assertThat(url.getQueryParameters().get("key")).as("'key' property value").isEqualTo(""); } @Test - public void testTmpfsOption() { + void testTmpfsOption() { String urlString = "jdbc:tc:mysql://somehostname/databasename?TC_TMPFS=key:value,key1:value1"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -69,7 +65,7 @@ public void testTmpfsOption() { } @Test - public void testInitScriptPathCapture() { + void testInitScriptPathCapture() { String urlString = "jdbc:tc:mysql:8.0.36://somehostname:3306/databasename?a=b&c=d&TC_INITSCRIPT=somepath/init_mysql.sql"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -83,13 +79,14 @@ public void testInitScriptPathCapture() { .containsEntry("TC_INITSCRIPT", "somepath/init_mysql.sql"); //Parameter sets are unmodifiable - thrown.expect(UnsupportedOperationException.class); - url.getContainerParameters().remove("TC_INITSCRIPT"); - url.getQueryParameters().remove("a"); + assertThatThrownBy(() -> url.getContainerParameters().remove("TC_INITSCRIPT")) + .isInstanceOf(UnsupportedOperationException.class); + assertThatThrownBy(() -> url.getQueryParameters().remove("a")) + .isInstanceOf(UnsupportedOperationException.class); } @Test - public void testInitFunctionCapture() { + void testInitFunctionCapture() { String urlString = "jdbc:tc:mysql:8.0.36://somehostname:3306/databasename?a=b&c=d&TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -105,7 +102,7 @@ public void testInitFunctionCapture() { } @Test - public void testDaemonCapture() { + void testDaemonCapture() { String urlString = "jdbc:tc:mysql:8.0.36://somehostname:3306/databasename?a=b&c=d&TC_DAEMON=true"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); @@ -113,7 +110,7 @@ public void testDaemonCapture() { } @Test - public void testHostLessConnectionUrl() { + void testHostLessConnectionUrl() { String urlString = "jdbc:tc:mysql:8.0.36:///databasename?a=b&c=d"; ConnectionUrl url = ConnectionUrl.newInstance(urlString); diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java index 4edd5d449ba..767258e19a1 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/ContainerDatabaseDriverTest.java @@ -1,9 +1,6 @@ package org.testcontainers.jdbc; -import org.hamcrest.CoreMatchers; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import java.sql.Connection; import java.sql.DriverManager; @@ -11,25 +8,23 @@ import java.util.Properties; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; -public class ContainerDatabaseDriverTest { +class ContainerDatabaseDriverTest { private static final String PLAIN_POSTGRESQL_JDBC_URL = "jdbc:postgresql://localhost:5432/test"; - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test - public void shouldNotTryToConnectToNonMatchingJdbcUrlDirectly() throws SQLException { + void shouldNotTryToConnectToNonMatchingJdbcUrlDirectly() throws SQLException { ContainerDatabaseDriver driver = new ContainerDatabaseDriver(); Connection connection = driver.connect(PLAIN_POSTGRESQL_JDBC_URL, new Properties()); assertThat(connection).isNull(); } @Test - public void shouldNotTryToConnectToNonMatchingJdbcUrlViaDriverManager() throws SQLException { - thrown.expect(SQLException.class); - thrown.expectMessage(CoreMatchers.startsWith("No suitable driver found for ")); - DriverManager.getConnection(PLAIN_POSTGRESQL_JDBC_URL); + void shouldNotTryToConnectToNonMatchingJdbcUrlViaDriverManager() throws SQLException { + assertThatThrownBy(() -> DriverManager.getConnection(PLAIN_POSTGRESQL_JDBC_URL)) + .isInstanceOf(SQLException.class) + .hasMessageStartingWith("No suitable driver found for "); } } diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/JdbcDatabaseDelegateTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/JdbcDatabaseDelegateTest.java index f3cf2d01d40..e6233e14b9f 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/JdbcDatabaseDelegateTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/JdbcDatabaseDelegateTest.java @@ -1,8 +1,7 @@ package org.testcontainers.jdbc; import lombok.NonNull; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.slf4j.Logger; import org.testcontainers.containers.JdbcDatabaseContainer; @@ -14,18 +13,19 @@ import java.util.ArrayList; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class JdbcDatabaseDelegateTest { +class JdbcDatabaseDelegateTest { @Test - public void testLeakedConnections() { + void testLeakedConnections() { final JdbcDatabaseContainerStub stub = new JdbcDatabaseContainerStub(DockerImageName.parse("something")); try (JdbcDatabaseDelegate delegate = new JdbcDatabaseDelegate(stub, "")) { delegate.execute("foo", null, 0, false, false); } - Assert.assertEquals(0, stub.openConnectionsList.size()); + assertThat(stub.openConnectionsList.size()).isZero(); } static class JdbcDatabaseContainerStub extends JdbcDatabaseContainer { diff --git a/modules/jdbc/src/test/java/org/testcontainers/jdbc/MissingJdbcDriverTest.java b/modules/jdbc/src/test/java/org/testcontainers/jdbc/MissingJdbcDriverTest.java index 9f6b354818d..469234e15a6 100644 --- a/modules/jdbc/src/test/java/org/testcontainers/jdbc/MissingJdbcDriverTest.java +++ b/modules/jdbc/src/test/java/org/testcontainers/jdbc/MissingJdbcDriverTest.java @@ -1,7 +1,7 @@ package org.testcontainers.jdbc; import com.google.common.base.Throwables; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.testcontainers.containers.JdbcDatabaseContainer; import org.testcontainers.utility.DockerImageName; @@ -12,10 +12,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; -public class MissingJdbcDriverTest { +class MissingJdbcDriverTest { @Test - public void shouldFailFastIfNoDriverFound() { + void shouldFailFastIfNoDriverFound() { final MissingDriverContainer container = new MissingDriverContainer(); try {