xdev-software / mockserver-neolight

A lightweight rewrite of the MockServer project with focus on simplicity, maintainability and Testcontainers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven latest version DockerHub latest version Build Quality Gate Status

MockServer NeoLight

A lightweight rewrite of the abandoned MockServer project with focus on simplicity, maintainability and Testcontainers.

Note

The full list of changes can be found in the changelog.
You may also have a look at the comparison with other frameworks.

Usage

In combination with Testcontainers

Besides a few differences the usage is mostly identical to the original project.

try(MockServerContainer container = new MockServerContainer())
{
  container.start();
  
  try(MockServerClient client = new MockServerClient(
    container.getHost(),
    container.getServerPort()))
  {
    String expectedResponse = "Test";
    // Setup expectation
    client.when(request("/test").withMethod("GET"))
      .respond(response().withBody(expectedResponse));
    
    HttpClient httpClient = HttpClient.newHttpClient();
    
    // Execute request
    HttpResponse<String> resp = httpClient.send(
      HttpRequest.newBuilder()
        .uri(URI.create(container.getEndpoint() + "/test"))
        .GET()
        .build(),
      HttpResponse.BodyHandlers.ofString());
    
    assertEquals(expectedResponse, resp.body());
  }
}
Example using forwarding/recording
try(MockServerContainer container = new MockServerContainer())
{
  container.start();
  
  try(MockServerClient client = new MockServerClient(
    container.getHost(),
    container.getServerPort()))
  {
    // Setup forwarding
    client.when(request("/"))
      .forward(HttpForward.forward().withHost("my-nginx.local"));
    
    HttpClient httpClient = HttpClient.newHttpClient();
    
    // Execute request
    HttpResponse<String> resp = httpClient.send(
      HttpRequest.newBuilder()
        .uri(URI.create(container.getEndpoint() + "/"))
        .GET()
        .build(),
      HttpResponse.BodyHandlers.ofString());
    
    assertTrue(resp.body().contains("Welcome to nginx!"));
    
    // You can also retrieve requests, expectations and responses
    String recorded =
      client.retrieveRecordedRequestsAndResponses(request("/"), Format.JSON);
    // or generate the code for writing them
    String codeToGenerateExpectation =
      client.retrieveRecordedExpectations(request("/"), Format.JAVA);
  }
}

The returned codeToGenerateExpectation will look like this:

new MockServerClient("localhost", 1080)
.when(
        request()
                .withMethod("GET")
                .withPath("/")
                ...,
        Times.once(),
        TimeToLive.unlimited(),
        0
)
.respond(
        response()
                .withStatusCode(200)
                .withReasonPhrase("OK")
                .withHeaders(...)
                .withBody("<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>...")
);
Required dependencies in pom.xml
<dependency>
   <groupId>software.xdev.mockserver</groupId>
   <artifactId>client</artifactId>
   <version>...</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>software.xdev.mockserver</groupId>
   <artifactId>testcontainers</artifactId>
   <version>...</version>
   <scope>test</scope>
</dependency>

Further documentation

MockServer also works really well together with a network failure simulation tools such as ToxiProxy.

Installation

Installation guide for the latest release

Module Distribution via
client
server


testcontainers

Support

If you need support as soon as possible and you can't wait for any pull request, feel free to use our support.

Contributing

See the contributing guide for detailed instructions on how to get started with our project.

Dependencies and Licenses

View the license of the current project or the docs that contain a dependency summary (per module)

About

A lightweight rewrite of the MockServer project with focus on simplicity, maintainability and Testcontainers

License:Apache License 2.0


Languages

Language:Java 99.8%Language:Dockerfile 0.2%