Skip to content

Commit c442c9f

Browse files
Address dev content review (#3)
* address dev content review * add explanation for using maven-dependency-plugin * Update README.adoc * address dev content review --------- Co-authored-by: Gilbert Kwan <[email protected]>
1 parent 7ecaa3a commit c442c9f

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

Diff for: README.adoc

+11-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
:page-guide-category: microprofile
1414
:page-essential: false
1515
: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
1717
: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.
1818
:guide-author: Open Liberty
1919
:page-tags: ['MicroProfile', 'Jakarta EE']
@@ -74,21 +74,14 @@ docker build -t postgres-sample .
7474

7575
The `finish` directory in the root of this guide contains the finished application. Give it a try before you proceed.
7676

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:
7878

7979
[role='command']
8080
```
8181
cd ../finish
8282
mvn package
8383
```
8484

85-
Run the following command to download or update to the latest Open Liberty Docker image:
86-
87-
[role='command']
88-
```
89-
docker pull icr.io/appcafe/open-liberty:full-java11-openj9-ubi
90-
```
91-
9285
Build the `inventory` Docker image with the following command:
9386

9487
[role='command']
@@ -132,7 +125,7 @@ Run the following command to start the PostgreSQL database, which runs the `post
132125

133126
[role='command']
134127
```
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
136129
```
137130

138131
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
155148

156149
[role='command']
157150
```
158-
mvn liberty:devc -DdockerRunOpts="-e POSTGRES_HOSTNAME=172.17.0.2" -DserverStartTimeout=240
151+
mvn liberty:devc -DdockerRunOpts="-e DB_HOSTNAME=172.17.0.2" -DserverStartTimeout=240
159152
```
160153

161154
You need to wait a while to let dev mode start. After you see the following message, your application server in dev mode is ready:
@@ -232,7 +225,7 @@ include::staging/src/test/java/it/io/openliberty/guides/inventory/SystemResource
232225

233226
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.
234227

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

237230
The [hotspot=testAddSystem file=0]`testAddSystem()` verifies the [hotspot=addSystem file=0]`addSystem` and [hotspot=listContents file=0]`listContents` endpoints.
238231

@@ -330,15 +323,15 @@ LibertyContainer.java
330323
include::finish/src/test/java/it/io/openliberty/guides/inventory/LibertyContainer.java[]
331324
----
332325

333-
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.
334327

335328
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^].
336329

337330
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.
338331

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

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

343336
=== Configuring Maven project
344337

@@ -359,7 +352,9 @@ include::finish/pom.xml[]
359352

360353
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.
361354

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

364359
Save the changes, and press the `enter/return` key in your console window to run the tests. You will see the following output:
365360

@@ -394,7 +389,6 @@ Also, run the following commands to stop the PostgreSQL container that was start
394389
[role='command']
395390
```
396391
docker stop postgres-container
397-
docker rm postgres-container
398392
```
399393

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

Diff for: finish/pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<artifactId>maven-war-plugin</artifactId>
100100
<version>3.3.2</version>
101101
</plugin>
102+
<!-- tag::maven-dependency-plugin[] -->
102103
<plugin>
103104
<groupId>org.apache.maven.plugins</groupId>
104105
<artifactId>maven-dependency-plugin</artifactId>
@@ -118,6 +119,7 @@
118119
</execution>
119120
</executions>
120121
</plugin>
122+
<!-- end::maven-dependency-plugin[] -->
121123
<plugin>
122124
<groupId>io.openliberty.tools</groupId>
123125
<artifactId>liberty-maven-plugin</artifactId>

Diff for: finish/src/test/java/it/io/openliberty/guides/inventory/SystemResourceIT.java

+8
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public class SystemResourceIT {
6262
// end::network1[]
6363

6464
// tag::postgresContainer[]
65+
// tag::GenericContainer[]
6566
private static GenericContainer<?> postgresContainer
67+
// end::GenericContainer[]
6668
= new GenericContainer<>(DB_IMAGE)
6769
// tag::network2[]
6870
.withNetwork(network)
@@ -75,7 +77,9 @@ public class SystemResourceIT {
7577
// end::postgresContainer[]
7678

7779
// tag::inventoryContainer[]
80+
// tag::LibertyContainer[]
7881
private static LibertyContainer inventoryContainer
82+
// end::LibertyContainer[]
7983
= new LibertyContainer(invImage, testHttps(), httpsPort, httpPort)
8084
.withEnv("DB_HOSTNAME", DB_HOST)
8185
// tag::network3[]
@@ -146,8 +150,12 @@ public static void setup() throws Exception {
146150
throw new Exception(
147151
"Postgres database is running locally. Stop it and retry.");
148152
} else {
153+
// tag::postgresContainerStart[]
149154
postgresContainer.start();
155+
// end::postgresContainerStart[]
156+
// tag::inventoryContainerStart[]
150157
inventoryContainer.start();
158+
// end::inventoryContainerStart[]
151159
urlPath = inventoryContainer.getBaseURL(getProtocol());
152160
}
153161
}

0 commit comments

Comments
 (0)