Quarkus MockServer

Quarkus MockServer 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>1.13.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>1.13.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

Configuration property

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

boolean

true

Enabled or disable log of the mock-server

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_LOG

boolean

false

The container image name to use, for container based DevServices providers.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_IMAGE_NAME

string

Optional fixed port the dev service will listen to.

If not defined, the port will be chosen randomly.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_PORT

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 quarkus-dev-service-mockserver label. The value is configured using the service-name property.

Container sharing is only used in dev mode.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_SHARED

boolean

true

The value of the quarkus-dev-service-mockserver label attached to the started container. This property is used when shared is set to true. In this case, before starting a container, Dev Services for Mockserver looks for a container with the quarkus-dev-service-mockserver label set to the configured value. If found, it will use this container instead of starting a new one. Otherwise, it starts a new container with the quarkus-dev-service-mockserver label set to the specified value.

This property is used when you need multiple shared MockServer servers.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_SERVICE_NAME

string

mock-server

MockServer configuration file.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_CONFIG_FILE

string

MockServer’s configuration class-path binding. Useful for the test and CI builds. When set to true, a test-container withClasspathResourceMapping method is used.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_CONFIG_CLASS_PATH

boolean

false

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 testcontainers.reuse.enable=true in your .testcontainers.properties file, to be stored in your home.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_REUSE

boolean

false

The configuration directory to mount in the container /config.

Environment variable: QUARKUS_MOCKSERVER_DEVSERVICES_CONFIG_DIR

string

Host of the MockServer

Environment variable: QUARKUS_MOCKSERVER_HOST

string

localhost

Port of the MockServer

Environment variable: QUARKUS_MOCKSERVER_PORT

string

1080

Endpoint of the MockServer

Environment variable: QUARKUS_MOCKSERVER_ENDPOINT

string

http://localhost:8080

Host of the MockServer for the MockServerClient

Environment variable: QUARKUS_MOCKSERVER_CLIENT_HOST

string

localhost

Port of the MockServer for the MockServerClient

Environment variable: QUARKUS_MOCKSERVER_CLIENT_PORT

string

1080