Testcontainers MockServer Extension with advanced testing capabilities.
Features:
Gradle
testImplementation "io.goodforgod:testcontainers-extensions-mockserver:0.12.1"
Maven
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-mockserver</artifactId>
<version>0.12.1</version>
<scope>test</scope>
</dependency>
Test with container start in PER_RUN
mode will look like:
@TestcontainersMockServer(mode = ContainerMode.PER_RUN)
class ExampleTests {
@ConnectionMockServer
private MockServerConnection connection;
@Test
void test() {
connection.client().when(HttpRequest.request()
.withMethod("GET")
.withPath("/get"))
.respond(HttpResponse.response()
.withStatusCode(200)
.withBody("OK"));
}
}
MockServerConnection
is an abstraction with asserting data in database container and easily manipulate container connection settings.
You can inject connection via @ConnectionMockServer
as field or method argument or manually create it from container or manual settings.
class ExampleTests {
@ConnectionMockServer
private MockServerConnection connection;
@Test
void test() {
connection().client().when(HttpRequest.request()
.withMethod("GET")
.withPath("/get"))
.respond(HttpResponse.response()
.withStatusCode(200)
.withBody("OK"));
}
}
@TestcontainersMockServer
- allow automatically start container with specified image in different modes without the need to configure it.
Available containers modes:
PER_RUN
- start container one time per test execution. (Containers must have same instance, e.g. compare by ==
)PER_CLASS
- start new container each test class.PER_METHOD
- start new container each test method.Simple example on how to start container per class, no need to configure container:
@TestcontainersMockServer(mode = ContainerMode.PER_CLASS)
class ExampleTests {
@Test
void test(@ConnectionMockServer MockServerConnection connection) {
assertNotNull(connection);
}
}
That’s all you need.
It is possible to customize image with annotation image
parameter.
Image also can be provided from environment variable:
@TestcontainersMockServer(image = "${MY_IMAGE_ENV|mockserver/mockserver:5.15.0}")
class ExampleTests {
@Test
void test() {
// test
}
}
Image syntax:
mockserver/mockserver:5.15.0
${MY_IMAGE_ENV}
${MY_IMAGE_ENV|mockserver/mockserver:5.15.0}
When you need to manually configure container with specific options, you can provide such container as instance that will be used by @TestcontainersMockServer
,
this can be done using @ContainerMockServer
annotation for container.
Example:
@TestcontainersMockServer(mode = ContainerMode.PER_CLASS)
class ExampleTests {
@ContainerMockServer
private static final MockServerContainer container = new MockServerContainer().withNetworkAliases("mymockserver");
@Test
void test(@ConnectionMockServer MockServerConnection connection) {
assertEquals("mymockserver", connection.paramsInNetwork().get().host());
}
}
In case you want to enable Network.SHARED for containers you can do this using network
& shared
parameter in annotation:
@TestcontainersMockServer(network = @Network(shared = true))
class ExampleTests {
@Test
void test() {
// test
}
}
Default alias
will be created by default, even if nothing was specified (depends on implementation).
You can provide also custom alias for container. Alias can be extracted from environment variable also or default value can be provided if environment is missing.
In case specified environment variable is missing default alias
will be created:
@TestcontainersMockServer(network = @Network(alias = "${MY_ALIAS_ENV|my_default_alias}"))
class ExampleTests {
@Test
void test() {
// test
}
}
Image syntax:
my-alias
${MY_ALIAS_ENV}
${MY_ALIAS_ENV|my-alias-default}
MockServerConnection
- can be injected to field or method parameter and used to communicate with running container via @ConnectionMockServer
annotation.
MockServerConnection
provides connection parameters, useful asserts, checks, etc. for easier testing.
Example:
@TestcontainersMockServer(mode = ContainerMode.PER_CLASS, image = "mockserver/mockserver:5.15.0")
class ExampleTests {
@ConnectionMockServer
private MockServerConnection connection;
@Test
void test() {
connection.client().when(HttpRequest.request()
.withMethod("GET")
.withPath("/get"))
.respond(HttpResponse.response()
.withStatusCode(200)
.withBody("OK"));
}
}
In case you want to use some external MockServer instance that is running in CI or other place for tests (due to docker limitations or other), you can use special environment variables and extension will use them to propagate connection and no MockServer containers will be running in such case.
Special environment variables:
EXTERNAL_TEST_MOCKSERVER_HOST
- MockServer instance host.EXTERNAL_TEST_MOCKSERVER_PORT
- MockServer instance port.This project licensed under the Apache License 2.0 - see the LICENSE file for details.