Tuesday, 6 July 2021

MockWebServer: Hello world application

In this post, I am going to explain how to mock a simple GET API using MockWebServer.

 

Step 1: Get an instance of MockWebServer.

MockWebServer mockWebServer = new MockWebServer();

 

Step 2: Enqueue the messages.

String msg1 = "Good Morning";
String msg2 = "Good Afternoon";
String msg3 = "Good Evening";

mockWebServer.enqueue(new MockResponse().setBody(msg1));
mockWebServer.enqueue(new MockResponse().setBody(msg2));
mockWebServer.enqueue(new MockResponse().setBody(msg3));

 

The first request is served by the first enqueued response; the second request by the second enqueued response and so on.

 

Step 3: Start the server on port 1234.

mockWebServer.start(1234);

 

Step 4: Get a URL for connecting to this server.

HttpUrl baseUrl = mockWebServer.url("/v1/welcome");

 

That’s it you are done, now when you hit the baseUrl for the first time, you will receive the message "Good Morning" and for the second time, you will receive the message "Good Afternoon" and so on..

 

Find the below working application.

 

SimpleRestClient.java

package com.sample.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class SimpleRestClient {

	private String url;

	public SimpleRestClient(String url) {
		this.url = url;
	}

	public String getRequest() throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();

		HttpGet httpget = new HttpGet(url);
		CloseableHttpResponse response = httpclient.execute(httpget);

		HttpEntity httpEntity = response.getEntity();

		InputStream is = httpEntity.getContent();
		String str = getBody(is);

		return str;

	}

	public static String getBody(InputStream is) throws IOException {
		try (InputStreamReader isReader = new InputStreamReader(is)) {
			BufferedReader reader = new BufferedReader(isReader);
			StringBuffer sb = new StringBuffer();
			String str;
			while ((str = reader.readLine()) != null) {
				sb.append(str);
			}

			return sb.toString();
		}

	}
}

 

HelloWorldTest.java

package mockwebserver;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.sample.app.SimpleRestClient;

import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

public class HelloWorldTest {

	@Test
	public void hello() throws IOException {
		try (MockWebServer mockWebServer = new MockWebServer();) {
			String msg1 = "Good Morning";
			String msg2 = "Good Afternoon";
			String msg3 = "Good Evening";

			mockWebServer.enqueue(new MockResponse().setBody(msg1));
			mockWebServer.enqueue(new MockResponse().setBody(msg2));
			mockWebServer.enqueue(new MockResponse().setBody(msg3));

			mockWebServer.start(1234);

			HttpUrl baseUrl = mockWebServer.url("/v1/welcome");

			SimpleRestClient simpleRestClient = new SimpleRestClient(baseUrl.toString());

			String body1 = simpleRestClient.getRequest();
			assertEquals(msg1, body1);

			String body2 = simpleRestClient.getRequest();
			assertEquals(msg2, body2);

			String body3 = simpleRestClient.getRequest();
			assertEquals(msg3, body3);

			mockWebServer.shutdown();
		}

	}

}

 

Run HelloWorldTest.java, you will see the test case results in junit window.

 

 



You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/okhttp3/mockwebserver/mockwebserver-demo

Previous                                                    Next                                                    Home

No comments:

Post a Comment