WireMock vs Other Frameworks
What is WireMock?
WireMock is a lightweight, flexible, open-source library for stubbing and mocking HTTP services. It allows developers to simulate and test RESTful web services without depending on an actual up and running server. WireMock uses an embedded HTTP server to provide stub HTTP endpoints that respond with specified JSON, XML or any other content types to deliver the expected response to a client service.
Component Test using WireMock
Please refer to the following article for steps.
Alternatives to WireMock
MockServer
MockServer is a Java-based mocking and testing framework that enables developers to mock HTTP/HTTPS, SMTP, and other network protocols. It provides a simple and easy-to-use API for creating and managing mock servers, which can be used to test client/server interactions in a variety of testing scenarios.
Add the MockServer library to your project using Maven pom.xml
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
Create a MockServer instance in your test class
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.junit.Before;
import org.junit.Test;
public class ComponentTest {
private ClientAndServer mockServer;
@Before
public void setupMockServer() {
mockServer = ClientAndServer.startClientAndServer(8080);
}
// Your tests here
}
Define mock HTTP endpoints using MockServer’s API
@Test
public void testMyMockedEndpoint() {
mockServer.when(
HttpRequest.request()
.withMethod("GET")
.withPath("/endpoint"))
.respond(
HttpResponse.response()
.withStatusCode(200)
.withBody("{ \"id\": 123, \"name\": \"Mocked Resource\" }")
);
// Your test code here
}
You can use MockServer’s API to verify that certain requests were made to your mock endpoint during your tests.
Rest-Assured
Rest-Assured is a Java library that provides an easier way to test RESTful web services. It offers a simpler and more expressive syntax as compared to Java’s built-in HTTP client API. Rest-Assured is built on top of the Apache HTTP library and provides developers with a domain-specific language (DSL) for testing RESTful web services.
Add the Rest-Assured dependency to your project using Maven pom.xml
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
Create a Rest-Assured instance in your test class
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.BeforeClass;
import org.junit.Test;
@BeforeClass
public static void setup() {
RestAssured.baseURI = "https://api.example.com";
}
Write a test method that sends an HTTP request to the API using Rest-Assured
@Test
public void testGetRequest() {
Response response = RestAssured.get("/users/1");
response.then()
.statusCode(200)
.assertThat()
.body("[0].id", equalTo(1),
"[0].name", equalTo("John Doe"),
"[0].email", equalTo("john.doe@example.com"));
}
Restito
Restito is a lightweight HTTP mocking library for Java. It provides an easy-to-use API for mocking HTTP endpoints and can be used for testing RESTful APIs
Add the Restito dependency to your project using Maven pom.xml
<dependency>
<groupId>com.xebialabs.restito</groupId>
<artifactId>restito</artifactId>
<version>0.9.3</version>
<scope>test</scope>
</dependency>
Create a new Restito server
import static com.xebialabs.restito.builder.stub.StubHttp.*;
import static org.junit.Assert.*;
import org.glassfish.grizzly.http.Method;
import org.junit.*;
import javax.ws.rs.core.MediaType;
import java.util.Collections;
public class RestitoTests {
private StubServer server;
@Before
public void setUp() {
server = new StubServer().run();
}
@After
public void tearDown() {
server.stop();
}
}
Write a test method that sends an HTTP request to the API using Restito
@Test
public void testMockEndpoint() throws Exception {
String expectedJson = "{\"name\": \"John\", \"id\": \"1\"}";
whenHttp(server)
.match(get("/users/1"))
.then(status(HttpStatus.OK_200), stringContent(expectedJson));
}
Advantages on WireMock
- Easy setup: WireMock is most widely used across the industry and in open source. Due to this fact it has good documentation around it and community it is easier to set up.
- Supports asynchronous services: WireMock includes support for asynchronous services. Can be used to mock and test services that use non-blocking I/O.
- Provides advanced request matching: Help in creating robust and accurate testing.
- Supports dynamic responses: Create complex responses based on the input received from clients. It also supports stateful responses, which can help developers to test stateful services.