Skip to content

Commit 87487b0

Browse files
committed
#13 Upgrade to testcontainers-java 2.0.3
- Remove JUnit 4 use - Update README
1 parent 1e7be42 commit 87487b0

File tree

5 files changed

+148
-86
lines changed

5 files changed

+148
-86
lines changed

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Version History
22
===============
33

4+
2.0.0-SNAPSHOT
5+
-----
6+
- Updated to org.testcontainers:testcontainers-jdbc 2.0.3 \
7+
Testcontainers-java 2.0.x no longer supports JUnit 4 rules. You need to either:
8+
- switch to JUnit 5 and use the `@Testcontainers` and `@Container` annotations,
9+
- stay on firebird-testcontainers-java 1.6.1 and org.testcontainers:jdbc 1.21.4,
10+
- switch to using Testcontainers URLs instead of using `@Rule` or `@ClassRule`, or
11+
- write you own rule implementation to start and stop the container.
12+
- Updated various test dependencies
13+
414
1.6.1
515
-----
616
- Updated org.testcontainers:jdbc to 1.21.4 \

README.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ Usage
5050

5151
For extensive documentation, consult https://www.testcontainers.org/modules/databases/
5252

53-
### JUnit rule
53+
### JUnit support
5454

55-
Using a JUnit `@Rule` or `@ClassRule` you can configure a container and start it
56-
per test (`@Rule`) or per class (`@ClassRule`).
55+
Starting with `firebird-testcontainers-java` 2.0.0, use of JUnit 4 is no longer
56+
supported directly. If you still need JUnit 4 support, use `firebird-testcontainers-java`
57+
1.6.1, use URL based, or derive your own rule implementation to start and stop
58+
the container.
59+
60+
For JUnit 5 support, add `org.testcontainers:testcontainers-junit-jupiter` as
61+
a test dependency. Annotate the test class with `@Testcontainers`. Define
62+
a `FirebirdContainer` static (shared by all tests), or instance (per test)
63+
field. Annotate this field with `@Container`.
5764

5865
The container defines several `withXXX` methods for configuration.
5966

@@ -81,19 +88,26 @@ sets docker environment variable `ISC_PASSWORD` (`jacobalberty/firebird`) or `FI
8188
Example of use:
8289

8390
```java
91+
package org.firebirdsql.testcontainers.examples;
92+
8493
import org.firebirdsql.testcontainers.FirebirdContainer;
85-
import org.testcontainers.utility.DockerImageName;
94+
import org.junit.jupiter.api.Test;
95+
import org.testcontainers.junit.jupiter.Container;
96+
import org.testcontainers.junit.jupiter.Testcontainers;
97+
98+
import java.sql.*;
99+
100+
import static org.firebirdsql.testcontainers.FirebirdTestImages.FIREBIRD_TEST_IMAGE;
101+
import static org.junit.jupiter.api.Assertions.*;
86102

87103
/**
88-
* Simple test demonstrating use of {@code @Rule}.
104+
* Simple test demonstrating use of {@code @Testcontainers} and {@code @Container}.
89105
*/
90-
public class ExampleRuleTest {
106+
@Testcontainers
107+
public class ExampleContainerTest {
91108

92-
private static final DockerImageName IMAGE =
93-
DockerImageName.parse(FirebirdContainer.IMAGE).withTag("5.0.3");
94-
95-
@Rule
96-
public final FirebirdContainer<?> container = new FirebirdContainer<?>(IMAGE)
109+
@Container
110+
public final FirebirdContainer<?> container = new FirebirdContainer<>(FIREBIRD_TEST_IMAGE)
97111
.withUsername("testuser")
98112
.withPassword("testpassword");
99113

@@ -103,8 +117,8 @@ public class ExampleRuleTest {
103117
.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword());
104118
Statement stmt = connection.createStatement();
105119
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
106-
assertTrue("has row", rs.next());
107-
assertEquals("user name", "TESTUSER", rs.getString(1));
120+
assertTrue(rs.next(), "has row");
121+
assertEquals("TESTUSER", rs.getString(1), "user name");
108122
}
109123
}
110124
}
@@ -139,24 +153,33 @@ images for backwards compatibility.
139153
Example of use:
140154

141155
```java
156+
import org.junit.jupiter.api.Test;
157+
158+
import java.sql.*;
159+
160+
import static org.junit.jupiter.api.Assertions.*;
161+
142162
/**
143163
* Simple test demonstrating use of url to instantiate container.
144164
*/
145-
public class ExampleUrlTest {
146-
147-
@Test
148-
public void canConnectUsingUrl() throws Exception {
149-
try (Connection connection = DriverManager
150-
.getConnection("jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd");
151-
Statement stmt = connection.createStatement();
152-
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
153-
assertTrue("has row", rs.next());
154-
assertEquals("user name", "SOMEUSER", rs.getString(1));
155-
}
165+
class ExampleUrlTest {
166+
167+
@Test
168+
void canConnectUsingUrl() throws Exception {
169+
try (Connection connection = DriverManager
170+
.getConnection("jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd");
171+
Statement stmt = connection.createStatement();
172+
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
173+
assertTrue(rs.next(), "has row");
174+
assertEquals("SOMEUSER", rs.getString(1), "user name");
156175
}
176+
}
157177
}
158178
```
159179

180+
For this type of use, it is not necessary to add `org.testcontainers:testcontainers-junit-jupiter`
181+
as a test dependency.
182+
160183
License
161184
-------
162185

pom.xml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
<maven.compiler.source>1.8</maven.compiler.source>
3838
<maven.compiler.target>1.8</maven.compiler.target>
3939

40-
<testcontainers.version>1.21.4</testcontainers.version>
40+
<testcontainers.version>2.0.3</testcontainers.version>
4141
<jaybird.version>5.0.11.java8</jaybird.version>
42-
<junit5.version>5.13.4</junit5.version>
42+
<junit5.version>5.14.3</junit5.version>
4343
</properties>
4444

4545
<dependencyManagement>
@@ -51,31 +51,42 @@
5151
<type>pom</type>
5252
<scope>import</scope>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.testcontainers</groupId>
56+
<artifactId>testcontainers-bom</artifactId>
57+
<version>${testcontainers.version}</version>
58+
<type>pom</type>
59+
<scope>import</scope>
60+
</dependency>
5461
</dependencies>
5562
</dependencyManagement>
5663

5764
<dependencies>
5865
<dependency>
5966
<groupId>org.testcontainers</groupId>
60-
<artifactId>jdbc</artifactId>
61-
<version>${testcontainers.version}</version>
62-
</dependency>
63-
<dependency>
64-
<groupId>junit</groupId>
65-
<artifactId>junit</artifactId>
66-
<version>4.13.2</version>
67+
<artifactId>testcontainers-jdbc</artifactId>
6768
</dependency>
6869

6970
<!-- Test dependencies may be Java 11+ -->
7071

72+
<dependency>
73+
<groupId>org.testcontainers</groupId>
74+
<artifactId>testcontainers-junit-jupiter</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
7178
<dependency>
7279
<groupId>org.junit.jupiter</groupId>
7380
<artifactId>junit-jupiter</artifactId>
74-
<scope>test</scope>
7581
</dependency>
7682
<dependency>
77-
<groupId>org.junit.vintage</groupId>
78-
<artifactId>junit-vintage-engine</artifactId>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter-params</artifactId>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.hamcrest</groupId>
88+
<artifactId>hamcrest</artifactId>
89+
<version>3.0</version>
7990
<scope>test</scope>
8091
</dependency>
8192

src/test/java/org/firebirdsql/testcontainers/JDBCDriverTest.java

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,75 @@
33
import com.zaxxer.hikari.HikariConfig;
44
import com.zaxxer.hikari.HikariDataSource;
55
import org.apache.commons.dbutils.QueryRunner;
6-
import org.junit.AfterClass;
7-
import org.junit.Test;
8-
import org.junit.runner.RunWith;
9-
import org.junit.runners.Parameterized;
10-
import org.junit.runners.Parameterized.Parameter;
6+
import org.junit.jupiter.api.AfterAll;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedClass;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
1111
import org.testcontainers.jdbc.ContainerDatabaseDriver;
1212

1313
import javax.sql.DataSource;
1414
import java.sql.Connection;
1515
import java.sql.SQLException;
1616
import java.util.EnumSet;
17+
import java.util.Set;
18+
import java.util.stream.Stream;
1719

1820
import static java.util.Arrays.asList;
1921
import static org.hamcrest.CoreMatchers.endsWith;
2022
import static org.hamcrest.MatcherAssert.assertThat;
21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2325

24-
@RunWith(Parameterized.class)
26+
@ParameterizedClass(name = "{index} - {0}")
27+
@MethodSource("data")
2528
public class JDBCDriverTest {
2629

30+
// NOTE Class needs to be public due to sampleInitFunction referenced in test URLs
31+
2732
private enum Options {
2833
ScriptedSchema,
2934
JDBCParams,
3035
}
3136

32-
@Parameter
33-
public String jdbcUrl;
34-
@Parameter(1)
35-
public EnumSet<Options> options;
36-
37-
@Parameterized.Parameters(name = "{index} - {0}")
38-
public static Iterable<Object[]> data() {
39-
return asList(
40-
new Object[][]{
41-
{"jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
42-
{"jdbc:tc:firebirdsql://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
43-
{"jdbc:tc:firebird:latest://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
44-
{"jdbc:tc:firebird:5://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
45-
{"jdbc:tc:firebird:5.0.3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
46-
{"jdbc:tc:firebirdsql:5.0.3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
47-
{"jdbc:tc:firebird:4://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
48-
{"jdbc:tc:firebird:4.0.6://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
49-
{"jdbc:tc:firebird:3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
50-
{"jdbc:tc:firebird:3.0.13://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
37+
private final String jdbcUrl;
38+
private final Set<Options> options;
39+
40+
JDBCDriverTest(String jdbcUrl, Set<Options> options) {
41+
this.jdbcUrl = jdbcUrl;
42+
this.options = options;
43+
}
44+
45+
static Stream<Arguments> data() {
46+
return Stream.of(
47+
testCase("jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
48+
testCase("jdbc:tc:firebirdsql://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
49+
testCase("jdbc:tc:firebird:latest://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
50+
testCase("jdbc:tc:firebird:5://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
51+
testCase("jdbc:tc:firebird:5.0.3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
52+
testCase("jdbc:tc:firebirdsql:5.0.3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
53+
testCase("jdbc:tc:firebird:4://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
54+
testCase("jdbc:tc:firebird:4.0.6://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
55+
testCase("jdbc:tc:firebird:3://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
56+
testCase("jdbc:tc:firebird:3.0.13://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
5157

5258
// jacobalberty tags are mapped
53-
{"jdbc:tc:firebird:v4.0.2://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
54-
{"jdbc:tc:firebird:v3.0.10://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
59+
testCase("jdbc:tc:firebird:v4.0.2://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
60+
testCase("jdbc:tc:firebird:v3.0.10://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams)
5561
// These images are problematic (they don't always seem to have their port available, or connecting doesn't work)
56-
//{"jdbc:tc:firebird:2.5.9-sc://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
57-
//{"jdbc:tc:firebird:2.5.9-ss://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
58-
});
62+
//testCase("jdbc:tc:firebird:2.5.9-sc://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
63+
//testCase("jdbc:tc:firebird:2.5.9-ss://hostname/databasename?user=someuser&password=somepwd&charSet=utf-8&TC_INITFUNCTION=org.firebirdsql.testcontainers.JDBCDriverTest::sampleInitFunction", Options.ScriptedSchema, Options.JDBCParams),
64+
);
65+
}
66+
67+
@SuppressWarnings("SameParameterValue")
68+
private static Arguments testCase(String jdbcUrl, Options... options) {
69+
Set<Options> optionSet = EnumSet.noneOf(Options.class);
70+
optionSet.addAll(asList(options));
71+
return Arguments.of(jdbcUrl, optionSet);
5972
}
6073

74+
// Must be public: referenced in test URLs
6175
@SuppressWarnings("unused")
6276
public static void sampleInitFunction(Connection connection) throws SQLException {
6377
connection.createStatement().execute("CREATE TABLE bar (\n" +
@@ -69,13 +83,13 @@ public static void sampleInitFunction(Connection connection) throws SQLException
6983
");");
7084
}
7185

72-
@AfterClass
73-
public static void testCleanup() {
86+
@AfterAll
87+
static void testCleanup() {
7488
ContainerDatabaseDriver.killContainers();
7589
}
7690

7791
@Test
78-
public void test() throws SQLException {
92+
void test() throws SQLException {
7993
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
8094
performSimpleTest(dataSource);
8195

@@ -93,18 +107,19 @@ private void performSimpleTest(DataSource dataSource) throws SQLException {
93107
boolean result = new QueryRunner(dataSource).query("SELECT 1 FROM RDB$DATABASE", rs -> {
94108
rs.next();
95109
int resultSetInt = rs.getInt(1);
96-
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
110+
assertEquals(1, resultSetInt, "A basic SELECT query succeeds");
97111
return true;
98112
});
99113

100-
assertTrue("The database returned a record as expected", result);
114+
assertTrue(result, "The database returned a record as expected");
101115
}
102116

103117
private void performTestForScriptedSchema(DataSource dataSource) throws SQLException {
104118
new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", rs -> {
105119
rs.next();
106120
String resultSetString = rs.getString(1);
107-
assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString);
121+
assertEquals("hello world", resultSetString,
122+
"A basic SELECT query succeeds where the schema has been applied from a script");
108123
return true;
109124
});
110125
}
@@ -117,11 +132,11 @@ private void performTestForJDBCParamUsage(DataSource dataSource) throws SQLExcep
117132
if (resultUser.endsWith("@%")) {
118133
resultUser = resultUser.substring(0, resultUser.length() - 2);
119134
}
120-
assertEquals("User from query param is created.", "SOMEUSER", resultUser);
135+
assertEquals("SOMEUSER", resultUser, "User from query param is created");
121136
return true;
122137
});
123138

124-
assertTrue("The database returned a record as expected", result);
139+
assertTrue(result, "The database returned a record as expected");
125140

126141
String databaseQuery = "select rdb$get_context('SYSTEM', 'DB_NAME') from RDB$DATABASE";
127142

@@ -133,9 +148,10 @@ private void performTestForJDBCParamUsage(DataSource dataSource) throws SQLExcep
133148
return true;
134149
});
135150

136-
assertTrue("The database returned a record as expected", result);
151+
assertTrue(result, "The database returned a record as expected");
137152
}
138153

154+
@SuppressWarnings("SameParameterValue")
139155
private HikariDataSource getDataSource(String jdbcUrl, int poolSize) {
140156
HikariConfig hikariConfig = new HikariConfig();
141157
hikariConfig.setJdbcUrl(jdbcUrl);

0 commit comments

Comments
 (0)