Mocking a HTTP server with WireMock
TweetPosted on Saturday Feb 22, 2014 at 08:47AM in Technology
Environment
- WireMock 1.4.3
- HttpClient 4.2.3
- Oracle JDK7u51
Resources
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.nailedtothex</groupId> <artifactId>wiremock</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>1.43</version> <classifier>standalone</classifier> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.3.2</version> </dependency> </dependencies> </project>
HttpFetcher.java
package org.nailedtothex.wiremock; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.fluent.Request; public class HttpFetcher { public String fetchAsString(String url) throws ClientProtocolException, IOException { return Request.Get(url).execute().returnContent().asString(); } }
HttpFetcherTest.java
package org.nailedtothex.wiremock; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.apache.http.client.HttpResponseException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import com.github.tomakehurst.wiremock.junit.WireMockRule; public class HttpFetcherTest { @Rule public WireMockRule wireMockRule = new WireMockRule(18089); private HttpFetcher instance; @Before public void init() { instance = new HttpFetcher(); stubFor(get(urlEqualTo("/hoge.txt")).willReturn( aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody("hoge"))); stubFor(get(urlEqualTo("/500.txt")).willReturn( aResponse().withStatus(500).withHeader("Content-Type", "text/plain").withBody("hoge"))); stubFor(get(urlEqualTo("/503.txt")).willReturn( aResponse().withStatus(503).withHeader("Content-Type", "text/plain").withBody("hoge"))); } @Test public void ok() throws Exception { String actual = instance.fetchAsString("http://localhost:18089/hoge.txt"); String expected = "hoge"; assertThat(actual, is(expected)); } @Test(expected = HttpResponseException.class) public void notFound() throws Exception { instance.fetchAsString("http://localhost:18089/NOT_FOUND"); } @Test(expected = HttpResponseException.class) public void internalServerError() throws Exception { instance.fetchAsString("http://localhost:18089/500.txt"); } @Test(expected = HttpResponseException.class) public void serviceUnavailable() throws Exception { instance.fetchAsString("http://localhost:18089/503.txt"); } }
Remarks
- To check behavior of the mock with browsers, we can set an breakpoint in the test class to prevent shutdown of the mock HTTP server.
- I have checked that stub urls are working as I expected with firebug.
- WireMock dependency brings many its dependencies, but I guess that are not necessary because it is classified as standalone, so I just add exclusions and it is working without any problems anyway.
References
Tags: test