You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+11-17
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@
13
13
:page-guide-category: microprofile
14
14
:page-essential: false
15
15
:page-description: Learn how to test your microservices with multiple containers using Testcontainers and JUnit.
16
-
:page-seo-title: Testing Java microservices using Testcontainers and JUnit with multiple containers and OpenLiberty Docker container
16
+
:page-seo-title: Testing Java microservices using Testcontainers and JUnit with multiple containers and Open Liberty Docker container
17
17
:page-seo-description: A getting started tutorial on how to develop true-to-production integration tests for Java microservices in production-like settings by using Testcontainers and JUnit with multiple containers and Open Liberty Docker container.
The `finish` directory in the root of this guide contains the finished application. Give it a try before you proceed.
76
76
77
-
To try out the test, first go to the `finish` directory and run the `mvn package` command so that the `.war` file resides in the `target` directory:
77
+
To try out the test, first go to the `finish` directory and run the `mvn package` command so that the `.war` file resides in the `target` directory and the `.jar` PostgreSQL JDBC driver file resides in the `target/liberty/wlp/usr/shared/resources` directory:
78
78
79
79
[role='command']
80
80
```
81
81
cd ../finish
82
82
mvn package
83
83
```
84
84
85
-
Run the following command to download or update to the latest Open Liberty Docker image:
Build the `inventory` Docker image with the following command:
93
86
94
87
[role='command']
@@ -132,7 +125,7 @@ Run the following command to start the PostgreSQL database, which runs the `post
132
125
133
126
[role='command']
134
127
```
135
-
docker run --name postgres-container -p 5432:5432 -d postgres-sample
128
+
docker run --name postgres-container --rm -p 5432:5432 -d postgres-sample
136
129
```
137
130
138
131
The Open Liberty Maven plug-in includes a `devc` goal that simplifies developing your application in a container by starting development mode with container support, known as dev mode. This goal builds a Docker image, mounts the required directories, binds the required ports, and then runs the application inside of a container. Dev mode also listens for any changes in the application source code or configuration and rebuilds the image and restarts the container as necessary.
@@ -155,7 +148,7 @@ Build and run the container by running the `devc` goal with the PostgreSQL conta
The [hotspot=createRestClient file=0]`createRestClient()` method creates a REST client instance with the `SystemResourceClient` interface, and configures a hostname verifier if the tests run over HTTPS.
234
227
235
-
The [hotspot=setup file=0]`setup` method determines the protocol to use (HTTP or HTTPS) and builds the base URL path for the REST client.
228
+
The [hotspot=setup file=0]`setup()` method determines the protocol to use (HTTP or HTTPS) and builds the base URL path for the REST client.
236
229
237
230
The [hotspot=testAddSystem file=0]`testAddSystem()` verifies the [hotspot=addSystem file=0]`addSystem` and [hotspot=listContents file=0]`listContents` endpoints.
Define the [hotspot=postgresContainer file=0]`postgresContainer` test container to start up the PostgreSQL Docker image, and define the [hotspot=inventoryContainer file=0]`inventoryContainer` test container to start up the `inventory` Docker image. Because containers are isolated by default, make sure both containers use the same [hotspot=network1 hotspot=network2 hotspot=network3 file=0]`network` for them to communicate.
326
+
Use [hotspot=GenericContainer file=0]`GenericContainer` class to create the [hotspot=postgresContainer file=0]`postgresContainer` test container to start up the PostgreSQL Docker image, and use the [hotspot=LibertyContainer file=0]`LibertyContainer` custom class to create the [hotspot=inventoryContainer file=0]`inventoryContainer` test container to start up the `inventory` Docker image. Because containers are isolated by default, make sure both containers use the same [hotspot=network1 hotspot=network2 hotspot=network3 file=0]`network` for them to communicate.
334
327
335
328
The [hotspot=waitingFor file=0]`waitingFor()` method overrides the [hotspot=waitingFor file=1]`waitingFor()` method in [hotspot file=1]`LibertyContainer`, ensuring the `inventoryContainer` is ready before tests run by checking the [hotspot=waitingFor file=0]`/health/ready` health readiness check API. For different container readiness check customizations, refer to the https://www.testcontainers.org/features/startup_and_waits/[official Testcontainers documentation^].
336
329
337
330
The [hotspot=getLogger file=0]`LoggerFactory.getLogger()` and [hotspot=withLogConsumer1 hotspot=withLogConsumer2 file=0]`withLogConsumer(new Slf4jLogConsumer(Logger))` methods integrate container logs with the test logs by piping the container output to the specified logger.
338
331
339
-
The updated [hotspot=setup file=0]`setup` method prepares the test environment. It checks if the tests are running in dev mode or local runtime, or via Testcontainers, using the [hotspot=isServiceRunning file=0]`isServiceRunning()` helper. If it's in dev mode or local runtime, it ensures the Postgres database is running locally. In the case of Testcontainers, it starts Postgres and `inventory` containers.
332
+
The updated [hotspot=setup file=0]`setup()` method prepares the test environment. It checks if the tests are running in dev mode or local runtime, or via Testcontainers, using the [hotspot=isServiceRunning file=0]`isServiceRunning()` helper. If it's in dev mode or local runtime, it ensures the Postgres database is running locally. In the case of no running runtime, the test starts the [hotspot=postgresContainerStart file=0]`postgresContainer` and [hotspot=inventoryContainerStart file=0]`inventoryContainer` test containers.
340
333
341
-
After tests, the [hotspot=tearDown file=0]`tearDown` method stops the containers and closes the network.
334
+
After tests, the [hotspot=tearDown file=0]`tearDown()` method stops the containers and closes the network.
342
335
343
336
=== Configuring Maven project
344
337
@@ -359,7 +352,9 @@ include::finish/pom.xml[]
359
352
360
353
Add the required `dependency` for Testcontainers and Log4J libraries with `test` scope. The [hotspot=testcontainers file=0]`testcontainers` dependency offers a general-purpose API for managing container-based test environments. The [hotspot=slf4j file=0]`slf4j-reload4j` dependency enables the Simple Logging Facade for Java (SLF4J) API for trace logging during test execution and facilitates debugging and test performance tracking.
361
354
362
-
Also, add the [hotspot=failsafe file=0]`maven-failsafe-plugin` plugin, so that the integration test can be run by the Maven `verify` goal.
355
+
Because the [hotspot=maven-dependency-plugin file=0]`maven-dependency-plugin` in the Maven pom.xml file is configured to copy the PostgreSQL JDBC (Java Database Connectivity) driver into the Liberty server's shared resources directory during the `prepare-package` phase, running `mvn package` ensures the driver is available for your application when running on the Liberty server.
356
+
357
+
Also, add and configure the [hotspot=failsafe file=0]`maven-failsafe-plugin` plugin, so that the integration test can be run by the Maven `verify` goal.
363
358
364
359
Save the changes, and press the `enter/return` key in your console window to run the tests. You will see the following output:
365
360
@@ -394,7 +389,6 @@ Also, run the following commands to stop the PostgreSQL container that was start
394
389
[role='command']
395
390
```
396
391
docker stop postgres-container
397
-
docker rm postgres-container
398
392
```
399
393
400
394
Because you have already built the `inventory` Docker image in the *Try what you'll build* section, there's no need to build it again here.
0 commit comments