Skip to content

Commit 7abaa2c

Browse files
committed
Testcontainers/Java/JUnit 5: Add missing support. Disambiguate docs.
1 parent ac67c07 commit 7abaa2c

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

testing/testcontainers/java/README.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,25 @@ What's inside
2626

2727
This directory includes different canonical examples how to use those
2828
components within test harnesses of custom applications using `CrateDB`_.
29-
Currently, all test cases are based on JUnit 4. This is an enumeration
29+
Currently, all test cases are based on JUnit. This is an enumeration
3030
of examples you can explore here:
3131

32-
- ``TestClassScope``: Class-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
33-
- ``TestFunctionScope``: Function-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
32+
- ``TestClassScope``: Class-scoped testcontainer instance with automatic setup/teardown, see `Shared containers`_.
33+
- ``TestFunctionScope``: Function-scoped testcontainer instance with automatic setup/teardown, see `Restarted containers`_.
3434
- ``TestJdbcUrlScheme``: Database containers launched via Testcontainers "TC" JDBC URL scheme.
3535
- ``TestManual``: Function-scoped testcontainer instance with manual setup/teardown.
3636
- ``TestManualWithLegacyCrateJdbcDriver``:
3737
Function-scoped testcontainer instance with manual setup/teardown, using a custom
3838
``CrateDBContainer``, which uses the `legacy CrateDB JDBC driver`_.
39-
- ``TestSharedSingleton`` [1]:
40-
Testcontainer instance shared across multiple test classes, implemented using the Singleton pattern.
39+
- ``TestSharedSingleton``:
40+
Testcontainer instance shared across multiple test classes, implemented using the Singleton pattern, see `Singleton containers`_.
4141
- ``TestSharedSingletonEnvironmentVersion``:
4242
Testcontainer instance honoring the ``CRATEDB_VERSION`` environment variable, suitable
4343
for running a test matrix on different versions of CrateDB, shared across multiple test
4444
classes.
4545
- ``TestSqlInitialization``: Demonstrate different ways how Testcontainers can run an init script after
4646
the database container is started, but before your code initiates a connection to it.
4747

48-
[1]: Sometimes, it might be useful to define a container that is only started once for
49-
several test classes. There is no special support for this use case provided by
50-
the Testcontainers extension. Instead, this can be implemented using the Singleton
51-
pattern. See also `Testcontainers » Manual container lifecycle control » Singleton
52-
containers`_.
53-
5448

5549
*****
5650
Usage
@@ -89,6 +83,8 @@ Usage
8983
.. _CrateDB: https://github.com/crate/crate
9084
.. _CrateDB OCI image: https://hub.docker.com/_/crate
9185
.. _legacy CrateDB JDBC driver: https://crate.io/docs/jdbc/
86+
.. _Restarted containers: https://java.testcontainers.org/test_framework_integration/junit_5/#restarted-containers
87+
.. _Shared containers: https://java.testcontainers.org/test_framework_integration/junit_5/#shared-containers
88+
.. _Singleton containers: https://java.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers
9289
.. _Testcontainers for Java: https://github.com/testcontainers/testcontainers-java
9390
.. _Testcontainers CrateDB Module: https://www.testcontainers.org/modules/databases/cratedb/
94-
.. _Testcontainers » Manual container lifecycle control » Singleton containers: https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers

testing/testcontainers/java/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
testImplementation 'org.testcontainers:testcontainers:2.0.1'
3131
testImplementation 'org.testcontainers:testcontainers-cratedb:2.0.1'
3232
testImplementation 'org.testcontainers:testcontainers-postgresql:2.0.1'
33+
testImplementation 'org.testcontainers:testcontainers-junit-jupiter:2.0.1'
3334
}
3435

3536
java {

testing/testcontainers/java/src/test/java/io/crate/example/testing/TestClassScope.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.crate.example.testing.utils.TestingHelpers;
44
import org.junit.jupiter.api.Test;
55
import org.testcontainers.cratedb.CrateDBContainer;
6+
import org.testcontainers.junit.jupiter.Container;
7+
import org.testcontainers.junit.jupiter.Testcontainers;
68

79
import java.io.IOException;
810
import java.sql.SQLException;
@@ -11,19 +13,20 @@
1113

1214

1315
/**
14-
* Class-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
16+
* Class-scoped testcontainer instance with JUnit 5.
1517
* <p>
16-
* In case you can't use the URL support, or need to fine-tune the container, you can instantiate it yourself.
17-
* Note that if you use @Rule, you will be given an isolated container for each test method.
18-
* If you use @ClassRule, you will get on isolated container for all the methods in the test class.
18+
* The extension finds all fields that are annotated with @Container and calls their container
19+
* lifecycle methods (methods on the Startable interface).
20+
* Containers declared as static fields will be shared between test methods.
1921
* </p>
2022
* <p>
21-
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#database-container-objects"/>
22-
* <a href="https://www.testcontainers.org/test_framework_integration/junit_4/#ruleclassrule-integration"/>
23+
* <a href="https://java.testcontainers.org/test_framework_integration/junit_5/#shared-containers"/>
2324
* </p>
2425
*/
26+
@Testcontainers
2527
public class TestClassScope {
2628

29+
@Container
2730
public static CrateDBContainer cratedb = new CrateDBContainer(TestingHelpers.nameFromLabel("5.10"));
2831

2932
@Test

testing/testcontainers/java/src/test/java/io/crate/example/testing/TestFunctionScope.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.crate.example.testing.utils.TestingHelpers;
44
import org.junit.jupiter.api.Test;
55
import org.testcontainers.cratedb.CrateDBContainer;
6+
import org.testcontainers.junit.jupiter.Container;
7+
import org.testcontainers.junit.jupiter.Testcontainers;
68

79
import java.io.IOException;
810
import java.sql.SQLException;
@@ -11,19 +13,20 @@
1113

1214

1315
/**
14-
* Function-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
16+
* Function-scoped testcontainer instance with JUnit 5.
1517
* <p>
16-
* In case you can't use the URL support, or need to fine-tune the container, you can instantiate it yourself.
17-
* Note that if you use @Rule, you will be given an isolated container for each test method.
18-
* If you use @ClassRule, you will get on isolated container for all the methods in the test class.
18+
* The extension finds all fields that are annotated with @Container and calls their container
19+
* lifecycle methods (methods on the Startable interface).
20+
* Containers declared as instance fields will be started and stopped for every test method.
1921
* </p>
2022
* <p>
21-
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#database-container-objects"/>
22-
* <a href="https://www.testcontainers.org/test_framework_integration/junit_4/#ruleclassrule-integration" />
23+
* <a href="https://java.testcontainers.org/test_framework_integration/junit_5/#restarted-containers"/>
2324
* </p>
2425
*/
26+
@Testcontainers
2527
public class TestFunctionScope {
2628

29+
@Container
2730
public CrateDBContainer cratedb = new CrateDBContainer(TestingHelpers.nameFromLabel("5.10"));
2831

2932
@Test

0 commit comments

Comments
 (0)