Testing HTTP applications with a few ok libraries

David Herges
Jul 28, 2016 · 8 min read
A few Ok libraries — relevant part begins at 47:35, ends at ~53:00

Fetching messages from remote

GET /message?q=keyword
Host: localhost:8888
HTTP/1.1 200 Ok
{"text": "hello world!"}
public class Message {
public String text;
}
public interface MessagesApi { @GET("/message")
Call<Message> findMessage(@Query("q") String value);
}
Retrofit retrofit = new Retrofit.Builder()
.client(new OkHttpClient.Builder().build())
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl("http://localhost:8888")
.build();
MessagesApi ourApi = retrofit.create(MessagesApi.Class)

Talking to a web server

public class MessagesApiTest {  @Rule
public MockWebServer mockBackend = new MockWebServer();
@Test
public void everythingOk() {
MessagesApi msgApi = new Retrofit.Builder()
.client(new OkHttpClient.Builder().build())
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(mockBackend.url("/").toString())
.build()
.create(MessagesApi.class);
assertThat(msgApi).isNotNull();
}
}
okhttp3.mockwebserver.MockWebServer$3 execute
INFO: MockWebServer[52796] starting to accept connections
okhttp3.mockwebserver.MockWebServer$3 acceptConnections
INFO: MockWebServer[52796] done accepting connections: Socket closed

Time to add some real testing.

  @Test
public void findMessage_obtainsEntityObject() throws IOException {
mockBackend.enqueue(
new MockResponse().setBody("{\"text\":\"hello!\"}")
);
final Response<Message> response =
mockApi()
.findMessage("")
.execute();
final Message entity = response.body();
assertThat(entity.text).isEqualTo("hello!");
}
  @Test
public void findMessage_dispatchesExpectedRequest()
throws IOException, InterruptedException {
mockBackend.enqueue(
new MockResponse().setBody("{\"text\":\"hello!\"}"));
mockApi().findMessage("123").execute(); final RecordedRequest recordedRequest =
mockBackend.takeRequest();

assertThat(recordedRequest.getMethod())
.isEqualTo("GET");
assertThat(recordedRequest.getPath())
.isEqualTo("/message?q=123");
}

A little improvement

public interface MessagesApi {  @GET("message")
Call<Message> findMessage(@Query("q") String keyword);
class Builder {
private String baseUrl;
public Builder() {} public Builder baseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}
public MessagesApi build() {
return new Retrofit.Builder()
.client(new OkHttpClient.Builder().build())
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(baseUrl)
.build()
.create(MessagesApi.class);
}
}
}
  @Test
public void builder_returnsInstance() {
final MessagesApi messagesApi = new MessagesApi.Builder()
.baseUrl("http://localhost:8888")
.build();
assertThat(messagesApi).isNotNull();
}

Testing philosophy

Summary — tl;dr


David Herges

Written by

Web Application Developer at Agfa HealthCare. Front-end dev w/ Angular and TypeScript. Opinions are my own and not necessarily my employer‘s.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade