ajeydudhe / facile-mock

facile-mock is a simple mocking library for BrowserMobProxy to allow mocking the http requests in the tests. It allows running a given test with given scope as unit test or integration test. In case of the scope as integration test the mocking will be skipped altogether and run against actual server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status MIT licensed

facile-mock - Simple mocking library for BrowserMobProxy.

WORK IN PROGRESS Work in progress.

facile-mock is a simple mocking library for BrowserMobProxy to allow mocking the http requests in the tests. It allows running a given test with given scope as unit test or integration test. In case of the scope as integration test the mocking will be skipped altogether and run against actual server.

Adding the library reference

Add the maven dependency to your pom.xml as follows:

<dependency>
    <groupId>org.expedientframework.facilemock</groupId>
    <artifactId>facile-mock-http-proxy</artifactId>
    <version>1.0.0-M1</version>
</dependency>

Usage

try(HttpMockContext mock = HttpProxyManagerFactory.createMockContext(unitTest()))
{
  final int port = mock.getHttpProxyManager().getPort();
  mock.when(post(urlEquals("/dummy"))).then(respondWith("Hello from mock for post !!!"));
  final response = postAndGetResponseBody(port, "/dummy");
  assertThat(response).as("Response").isEqualTo("Hello from mock for post !!!");
}
  • HttpProxyManagerFactory.createMockContext() creates the mocking context indicating that we are executing unit tests so that the mocking should be honoured. If you mark it as integration tests then no mocking will be done and the http calls will be made to live server as per the url. So you can run same tests as unit tests with mocking and integration tests without mocking. You just need to passing in proper parameter to createMockContext() at runtime.
  • mock.when() takes the mocking condition which in this case says that for post request with url equal to /dummy we should return a mock response.
  • then() specifies that we should respond with a dummy string value.
  • The postAndGetResponseBody() method makes a http call with the given port on which the http proxy is listening as follows:
public int postAndGetResponseStatus(final int port, final String endpoint)
{
    try(CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("localhost", port)))
    {
      final HttpPost post = new HttpPost("http://sampleHostDoesNotExistsBlah.com" + endpoint);
      post.setEntity(new StringEntity("Dummy request body."));
      try(CloseableHttpResponse httpResponse = httpClient.execute(post))
      {
        return httpResponse.getStatusLine().getStatusCode();
      }
    }
    catch (IOException e)
    {
      throw new RuntimeException(e);
    }
}

About

facile-mock is a simple mocking library for BrowserMobProxy to allow mocking the http requests in the tests. It allows running a given test with given scope as unit test or integration test. In case of the scope as integration test the mocking will be skipped altogether and run against actual server.

License:MIT License


Languages

Language:Java 100.0%