Skip to content

Commit ce02533

Browse files
committed
Update docs to include imports
1 parent 334e451 commit ce02533

4 files changed

Lines changed: 111 additions & 60 deletions

File tree

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
[![Maven Central](https://img.shields.io/maven-central/v/org.microshed/microshed-testing-testcontainers.svg?label=Maven%20Central)](https://mvnrepository.com/artifact/org.microshed/microshed-testing-testcontainers)
44
[![Javadocs](https://www.javadoc.io/badge/org.microshed/microshed-testing-testcontainers.svg)](https://www.javadoc.io/doc/org.microshed/microshed-testing-testcontainers)
5-
[![Jitpack (Snapshots)](https://jitpack.io/v/microshed/microshed-testing.svg)](https://jitpack.io/#microshed/microshed-testing)
65
[![Website](https://img.shields.io/website/http/microshed.org/microshed-testing?up_color=informational)](http://microshed.org/microshed-testing)
76
[![Build Status](https://travis-ci.org/MicroShed/microshed-testing.svg?branch=master)](https://travis-ci.org/MicroShed/microshed-testing)
87
[![License](https://img.shields.io/badge/License-ASL%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
9-
[![Gitter](https://badges.gitter.im/MicroShed/microshed-testing.svg)](https://gitter.im/MicroShed/microshed-testing)
108

119
# Why use MicroShed Testing?
1210

@@ -63,6 +61,7 @@ NOTE: The first run will take longer due to downloading required container layer
6361
- Wildfly
6462
- Payara Micro
6563
- Apache TomEE
64+
- Quarkus
6665

6766
To change which app server is used, [un]comment sections of the test app's Dockerfile at `sample-apps/jaxrs-json/Dockerfile`
6867

@@ -101,6 +100,14 @@ public class PersonService {
101100
Using MicroShed Testing, we can write an integration test that looks something like this:
102101

103102
```java
103+
import static org.junit.jupiter.api.Assertions.*;
104+
import javax.ws.rs.NotFoundException;
105+
import org.junit.jupiter.api.Test;
106+
import org.microshed.testing.jaxrs.RESTClient;
107+
import org.microshed.testing.jupiter.MicroShedTest;
108+
import org.microshed.testing.testcontainers.ApplicationContainer;
109+
import org.testcontainers.junit.jupiter.Container;
110+
104111
@MicroShedTest
105112
public class BasicJAXRSServiceTest {
106113

docs/features/MP_JWT.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,18 @@ As the `@RolesAllowed` annotations imply, anyone can access the `GET /data/ping`
4141
When MicroShed Testing will automatically generate and configure a pair of JWT secrets for the `ApplicationContainer` container. Then a test client may access these endpoints using the `@JwtConfig` annotation on injected REST clients as follows:
4242

4343
```java
44+
import javax.ws.rs.ForbiddenException;
45+
import javax.ws.rs.NotAuthorizedException;
46+
47+
import org.junit.jupiter.api.Test;
48+
import org.microshed.testing.jaxrs.RESTClient;
49+
import org.microshed.testing.jupiter.MicroShedTest;
50+
import org.microshed.testing.jwt.JwtConfig;
51+
import org.microshed.testing.testcontainers.ApplicationContainer;
52+
import org.testcontainers.junit.jupiter.Container;
53+
4454
@MicroShedTest
45-
public class SecuredSvcTest {
55+
public class SecuredSvcIT {
4656

4757
@Container
4858
public static ApplicationContainer app = new ApplicationContainer()

docs/features/Walkthrough.md

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Have you ever toiled with creating mock objects for unit tests? How about custom
88

99
One of the great benefits of Docker is that we get a nice consistent package that contains everything down to the OS, meaning it’s portable to any hardware. Great, so lets use this to get consistent testing environments too!
1010

11-
# Starting point
11+
# Starting application
1212

1313
Assume we have a basic JAX-RS application that can perform create, update, and delete
1414
operations on 'Person' data objects. It may look something like this:
@@ -45,7 +45,7 @@ Now assume we also have simple Dockerfile in our repository that packages up our
4545
```
4646
FROM openliberty/open-liberty:full-java8-openj9-ubi
4747
COPY src/main/liberty/config /config/
48-
ADD build/libs/myservice.war /config/dropins
48+
ADD target/myservice.war /config/dropins
4949
```
5050

5151
It doesn't really matter what's in the Dockerfile. What matters is we can start it using Docker and interact with it over HTTP or some other protocol.
@@ -75,13 +75,48 @@ Given the above application code, we can start by adding maven dependencies:
7575
</dependencies>
7676
```
7777

78+
## Enabling JUnit Jupiter with Maven Failsafe
79+
80+
If you have never used JUnit Jupiter (JUnit 5) before with integration tests, there are a few important things to note:
81+
82+
1. The package import for `@Test` is now `import org.junit.jupiter.api.Test;`
83+
2. By default, test class names must match the pattern: `**/IT*.java`, `**/*IT.java`, or `**/*ITCase.java`
84+
3. The `maven-failsafe-plugin` must be added to your `pom.xml` with configuration similar to the following:
85+
86+
```xml
87+
<project>
88+
[...]
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-failsafe-plugin</artifactId>
94+
<version>2.22.0</version>
95+
<executions>
96+
<execution>
97+
<goals>
98+
<goal>integration-test</goal>
99+
<goal>verify</goal>
100+
</goals>
101+
</execution>
102+
</executions>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
[...]
107+
</project>
108+
```
109+
78110
## Starting the application container
79111

80112
Next, we create the basic test class and inject the REST endpoint we want to test:
81113

82114
```java
115+
import org.microshed.testing.jaxrs.RESTClient;
116+
import org.microshed.testing.jupiter.MicroShedTest;
117+
83118
@MicroShedTest
84-
public class MyTest {
119+
public class MyServiceIT {
85120

86121
@RESTClient
87122
public static MyService mySvc;
@@ -101,8 +136,13 @@ Launching defaultServer (Open Liberty 19.0.0.8/wlp-1.0.31.cl190820190813-1136) o
101136
Here we can see that the application is available at `http://localhost:9080/myservice/`, which means the context root is `/myservice`. Now we can add that information to the test class like so:
102137

103138
```java
139+
import org.microshed.testing.jaxrs.RESTClient;
140+
import org.microshed.testing.jupiter.MicroShedTest;
141+
import org.microshed.testing.testcontainers.ApplicationContainer;
142+
import org.testcontainers.junit.jupiter.Container;
143+
104144
@MicroShedTest
105-
public class MyTest {
145+
public class MyServiceIT {
106146

107147
@Container
108148
public static ApplicationContainer app = new ApplicationContainer()
@@ -121,36 +161,9 @@ org.testcontainers.containers.ContainerLaunchException:
121161
```
122162

123163
A few questions may come up at this point, such as:
124-
- Where are the logs??
125164
- Why port 33735?
126165
- Why wasn't the URL accessible?
127166

128-
### Where are the logs??
129-
130-
By default, containers pipe logs to SLF4j. This is certainly worth setting up, because coallates all container logs to the test output. This makes it easy to correlate what was going on inside of your application during each test method. One way to set up SFL4j is by adding the following dependency:
131-
132-
```xml
133-
<dependency>
134-
<groupId>org.slf4j</groupId>
135-
<artifactId>slf4j-log4j12</artifactId>
136-
<version>1.7.26</version>
137-
<scope>test</scope>
138-
</dependency>
139-
```
140-
141-
And then create a file at `src/test/resources/log4j.properties` with a valid Log4j configuration. For example:
142-
143-
```
144-
log4j.rootLogger=INFO, stdout
145-
146-
log4j.appender=org.apache.log4j.ConsoleAppender
147-
log4j.appender.layout=org.apache.log4j.PatternLayout
148-
149-
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
150-
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
151-
log4j.appender.stdout.layout.ConversionPattern=%r %p %c %x - %m%n
152-
```
153-
154167
### Why port 33735?
155168

156169
Although port 33735 was not configured anywhere, MicroShed Testing still waited for this port to be available. This is because the application is running inside a container, and the ports inside containers can be mapped to different ports outside of the container. Testcontainers takes advantage of this
@@ -162,10 +175,10 @@ By default, MicroShed Testing will poll the application container via HTTP on it
162175
However, our application does not respond at this endpoint, so we need to configure a different endpoint for readiness. Since the `getAllPeople()` method is bound to the `GET /myservice/people/` endpoint and does not depend on any particular state, it is a good candidate for a readiness check. We can configure the readiness check endpoint like this:
163176

164177
```java
165-
@Container
166-
public static ApplicationContainer app = new ApplicationContainer()
167-
.withAppContextRoot("/myservice")
168-
.withReadinessPath("/myservice/people");
178+
@Container
179+
public static ApplicationContainer app = new ApplicationContainer()
180+
.withAppContextRoot("/myservice")
181+
.withReadinessPath("/myservice/people");
169182
```
170183

171184
Alternatively, if your application runtime supports MicroProfile Health 2.0, it will have a standard readiness endpoint at `/heath/ready`, which will return `HTTP 200` when the application is available.
@@ -176,38 +189,44 @@ Now that the setup is complete, we are ready to write some test methods! First w
176189
reading the result back.
177190

178191
```java
179-
@Test
180-
public void testGetPerson() {
181-
// This invokes an HTTP POST request to the running container, which triggers
182-
// the PersonService#createPerson endpoint and returns the generated ID
183-
Long bobId = personSvc.createPerson("Bob", 24);
184-
185-
// Using the generated ID, invoke an HTTP GET request to read the record we just created
186-
// The JSON response will be automatically converted to a 'Person' object using JSON-B
187-
Person bob = personSvc.getPerson(bobId);
188-
189-
assertEquals("Bob", bob.name);
190-
assertEquals(24, bob.age);
191-
assertNotNull(bob.id);
192-
}
192+
import org.junit.jupiter.api.Test;
193+
// ...
194+
195+
@Test
196+
public void testGetPerson() {
197+
// This invokes an HTTP POST request to the running container, which triggers
198+
// the PersonService#createPerson endpoint and returns the generated ID
199+
Long bobId = personSvc.createPerson("Bob", 24);
200+
201+
// Using the generated ID, invoke an HTTP GET request to read the record we just created
202+
// The JSON response will be automatically converted to a 'Person' object using JSON-B
203+
Person bob = personSvc.getPerson(bobId);
204+
205+
assertEquals("Bob", bob.name);
206+
assertEquals(24, bob.age);
207+
assertNotNull(bob.id);
208+
}
193209
```
194210

195211
Next, we can write a negative test case that checks behavior for when someone requests a `Person` with an invalid ID.
196212

197213
```java
198-
@Test
199-
public void testGetUnknownPerson() {
200-
// This invokes an HTTP GET request to get a person with ID -1, which does not exist
201-
// asserts that the application container returns an HTTP 404 (not found) exception
202-
assertThrows(NotFoundException.class, () -> personSvc.getPerson(-1L));
203-
}
214+
import org.junit.jupiter.api.Test;
215+
// ...
216+
217+
@Test
218+
public void testGetUnknownPerson() {
219+
// This invokes an HTTP GET request to get a person with ID -1, which does not exist
220+
// asserts that the application container returns an HTTP 404 (not found) exception
221+
assertThrows(NotFoundException.class, () -> personSvc.getPerson(-1L));
222+
}
204223
```
205224

206225
## Expanding the number of tests
207226

208227
If more than one test class is used for the same application container, it will save time to leave containers running across multiple test classes.
209228
This can be accomplished by moving `@Container` annotated fields to a separate class that implements `SharedContainerConfiguration`.
210229

211-
For more information on this approach, see the [SharedContainerConfiguration documentation](01_SharedContainerConfiguration).
230+
For more information on this approach, see the [SharedContainerConfiguration documentation](SharedContainerConfiguration).
212231

213232

docs/index.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public class PersonService {
5252
Using MicroShed Testing, we can write an integration test that looks something like this:
5353

5454
```java
55+
import static org.junit.jupiter.api.Assertions.*;
56+
import javax.ws.rs.NotFoundException;
57+
import org.junit.jupiter.api.Test;
58+
import org.microshed.testing.jaxrs.RESTClient;
59+
import org.microshed.testing.jupiter.MicroShedTest;
60+
import org.microshed.testing.testcontainers.ApplicationContainer;
61+
import org.testcontainers.junit.jupiter.Container;
62+
5563
@MicroShedTest
5664
public class BasicJAXRSServiceTest {
5765

@@ -109,7 +117,7 @@ automatically produce a testable container image roughly equivalent to the follo
109117
```
110118
FROM openliberty/open-liberty:full-java8-openj9-ubi
111119
COPY src/main/liberty/config /config/
112-
ADD build/libs/$APP_FILE /config/dropins
120+
ADD target/$APP_FILE /config/dropins
113121
```
114122
115123
## Quick Start
@@ -139,6 +147,11 @@ Once you have the above dependencies added, create a new test class with the fol
139147
1. Inject one or more `public static` JAX-RS resource classes
140148
141149
```java
150+
import org.microshed.testing.jaxrs.RESTClient;
151+
import org.microshed.testing.jupiter.MicroShedTest;
152+
import org.microshed.testing.testcontainers.ApplicationContainer;
153+
import org.testcontainers.junit.jupiter.Container;
154+
142155
@MicroShedTest
143156
public class MyTest {
144157
@@ -153,3 +166,5 @@ public class MyTest {
153166
}
154167
```
155168
169+
For a more complete introduction, see the [Walkthrough page](Walkthrough).
170+

0 commit comments

Comments
 (0)