- Simple to setup
- Work with any JavaEE or MicroProfile runtime
- Provide true-to-production tests that are easy to write and fast to run
Add jitpack.io repository configuration to your pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add system-test
and junit-jupiter
as test-scoped dependencies:
<dependencies>
<dependency>
<groupId>com.github.dev-tools-for-enterprise-java</groupId>
<artifactId>system-test</artifactId>
<version>v0.2-alpha</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<!-- other dependencies... -->
</dependencies>
./gradlew :system-test-jaxrs-json:test
./gradlew publishToMavenLocal
cd sample-apps/maven-app
mvn clean install
NOTE: The first run will take longer due to downloading required container layers. Subsequent runs will be faster.
- OpenLiberty / WAS Liberty
- Wildfly
- Payara Micro
- TomEE
To change which app server is used, [un]comment sections of the test app's Dockerfile at sample-apps/jaxrs-json/Dockerfile
import org.aguibert.testcontainers.framework.MicroProfileApplication;
import org.eclipse.microprofile.system.test.jupiter.MicroProfileTest;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@MicroProfileTest
public class BasicJAXRSServiceTest {
@Container // (1)
public static MicroProfileApplication app = new MicroProfileApplication()
.withAppContextRoot("/myservice");
@Inject // (2)
public static PersonService personSvc;
@Test
public void testGetPerson() {
Long bobId = personSvc.createPerson("Bob", 24);
Person bob = personSvc.getPerson(bobId); // (3)
assertEquals("Bob", bob.name);
assertEquals(24, bob.age);
assertNotNull(bob.id);
}
}
- Extend Testcontainers with a
MicroProfileApplication
class that can work for any JEE/MP implementation. By annotating with@Container
, Testcontainers will automatically find/build the Dockerfile in this project and start it, then wait for the application context root to be ready. - Use the
@Inject
annotation to create a REST Client proxy of thePersonService
class which is being tested. This is basically a convenience for the test client making HTTP requests on the server and then parsing back the response. - Easily invoke HTTP requests on the running server and have the response bound back into a POJO (or an exception class if an error occurred)