REST Mock API Server Example

Integration projects require APIs contract and implementation to carry out integration work. Once contract is defined you need API end point from other project/service to finish your work.
If you dont get it on time, you are kind of stuck, what can you do? Create your own API end point and finish your side of implementation by changing your url end point to something local. That local service should be your mock service which gives you desire results same as like actual APIs.

MockServer is one of such library which can allow you to mock your thirdparty or dependent APIs.

Take a look of below easy example.

import org.mockserver.client.server.*;
import org.mockserver.model.*;
import static org.mockserver.integration.ClientAndServer.*;
import static org.mockserver.model.HttpRequest.*;
import static org.mockserver.model.HttpResponse.*;
import static java.util.concurrent.TimeUnit.*;

public class DummyPOSTService {
	public static void main(String[] args) {
		MockServerClient mockServer = startClientAndServer(1100);

		mockServer
	        .when(
	        		request()
                    .withMethod("POST")
                    .withPath("/test")
                    .withHeader("authorization", "dummy")
	        )
	        .respond(
	        		response()
	        		.withStatusCode(200)
                    .withHeaders(
                            new Header("Content-Type", "application/json; charset=utf-8"),
                            new Header("Cache-Control", "public, max-age=86400")
                    )
                    .withBody("{ 'test': 'abc' }")
                    .withDelay(new Delay(SECONDS, 1))
	        );
	}
}

You need this jar in order to run above class.

<a href="http://search.maven.org/remotecontent?filepath=org/mock-server/mockserver-netty/3.10.4/mockserver-netty-3.10.4-jar-with-dependencies.jar">mockserver-netty-3.10.4-jar-with-dependencies</a>

mock-api-server

RestEasy 3.0.x implementation with your existing servlet based web application

Web.xml entry:

<context-param>
		<param-name>resteasy.servlet.mapping.prefix</param-name>
		<param-value>/rest</param-value>
	</context-param>
 <context-param>
		<param-name>resteasy.resources</param-name>
		<param-value>com.abc.xyz.UserREST</param-value>
	</context-param>
    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>  
    <servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

Your REST class:

package com.abc.xyz;

import javax.ws.rs.*;

@Path("/Users")
public class UserREST {

	@GET
	@Path("/getUserById")
	public String doGet(@QueryParam("userId") String userId, @HeaderParam("authorization") String sessionToValidate)
			throws Exception {
			return "SomeString";
	}
}

Minimum jar files needed from jboss resteasy 3.0.19 bundle:

resteasy-jaxrs-3.0.19.Final.jar
jboss-jaxrs-api_2.0_spec-1.0.0.Final.jar
jboss-logging-3.1.4.GA.jar

And call : http(s)://yourhost:port/yourapplication/rest/Users/getUserById?userId=xx
You may choose to pass your own authorization in header, use Postman, but you can ignore it also.