Quarkus MockServer
Quarkus [MockServer](https://mock-server.com/) extension for tests and local development.
Quarkus test and dev MockServer extension. |
Installation
If you want to use this extension, you need to add the io.quarkiverse.mockserver:quarkus-mockserver
extension first to your build file.
For instance, with Maven, add the following dependency to your POM file:
<dependency>
<groupId>io.quarkiverse.mockserver</groupId>
<artifactId>quarkus-mockserver-test</artifactId>
<version>0.6.0</version>
<scope>provided</scope>
</dependency>
Usage
This extension provides a quarkus.mockserver.endpoint
variable that points to the launched Docker container.
Example configuration for local development and testing:
%dev.quarkus.mockserver.devservices.config-file=src/test/resources/mockserver.properties
%dev.quarkus.mockserver.devservices.config-dir=src/test/resources/mockserver
%dev.quarkus.mockserver.devservices.log=true
%dev.activity-client/mp-rest/url=${quarkus.mockserver.endpoint}
%test.activity-client/mp-rest/url=${quarkus.mockserver.endpoint}
Testing
To use the extension for test, add the dependency to the target project:
<dependency>
<groupId>io.quarkiverse.mockserver</groupId>
<artifactId>quarkus-mockserver-test</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>
Test class example
import io.quarkiverse.mockserver.test.MockServerTestResource;
import io.quarkus.test.common.QuarkusTestResource;
@QuarkusTest
@QuarkusTestResource(MockServerTestResource.class)
public class BaseTest {
@InjectMockServerClient
MockServerClient mockServerClient;
@Test
public void test200() {
Map<String, Object> data = new HashMap<>();
data.put("key-A", "value-A");
data.put("key-B", 1);
// create mock rest endpoint
mockServerClient
.when(request()
.withPath("/activity/data/1")
.withMethod("POST"))
.respond(
httpRequest -> response()
.withStatusCode(200)
.withHeader("Content-Type", "application/json")
.withBody(JsonBody.json(data))
);
}
// ... test
}
We can reuse the test for the integration test.
import io.quarkus.test.junit.QuarkusIntegrationTest;
@QuarkusIntegrationTest
public class BaseIT extends BaseTest {
}
For more information check examples in the integration-tests
directory in this repo.
Extension Configuration Reference
Remove this section if you don’t have Quarkus configuration properties in your extension. |
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Type |
Default |
|
---|---|---|
If DevServices has been explicitly enabled or disabled. DevServices is generally enabled by default, unless there is an existing configuration present. When DevServices is enabled Quarkus will attempt to automatically configure and start a database when running in Dev or Test mode and when Docker is running. Environment variable: |
boolean |
|
Enabled or disable log of the mock-server Environment variable: |
boolean |
|
The container image name to use, for container based DevServices providers. Environment variable: |
string |
|
Optional fixed port the dev service will listen to. If not defined, the port will be chosen randomly. Environment variable: |
int |
|
Indicates if the MockServer server managed by Quarkus Dev Services is shared. When shared, Quarkus looks for running containers using label-based service discovery. If a matching container is found, it is used, and so a second one is not started. Otherwise, Dev Services for MockServer starts a new container.
The discovery uses the Environment variable: |
boolean |
|
The value of the Environment variable: |
string |
|
MockServer configuration file. Environment variable: |
string |
|
MockServer’s configuration class-path binding. Useful for the test and CI builds. When set to Environment variable: |
boolean |
|
Helper to define the stop strategy for containers created by DevServices. In particular, we don’t want to actually stop the containers when they have been flagged for reuse, and when the Testcontainers configuration has been explicitly set to allow container reuse. To enable reuse, ass Environment variable: |
boolean |
|
The configuration directory to mount in the container /config. Environment variable: |
string |
|
Host of the MockServer Environment variable: |
string |
|
Port of the MockServer Environment variable: |
string |
|
Endpoint of the MockServer Environment variable: |
string |
|
Host of the MockServer for the MockServerClient Environment variable: |
string |
|
Port of the MockServer for the MockServerClient Environment variable: |
string |
|