Facile Mock

The parent module for mocking framework.

License

License

GroupId

GroupId

org.expedientframework.facilemock
ArtifactId

ArtifactId

facile-mock
Last Version

Last Version

1.0.0-M1
Release Date

Release Date

Type

Type

pom
Description

Description

Facile Mock
The parent module for mocking framework.
Project URL

Project URL

https://github.com/ajeydudhe/facile-mock
Source Code Management

Source Code Management

https://github.com/ajeydudhe/facile-mock/tree/master

Download facile-mock

Filename Size
facile-mock-1.0.0-M1.pom 6 KB
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock/ -->
<dependency>
    <groupId>org.expedientframework.facilemock</groupId>
    <artifactId>facile-mock</artifactId>
    <version>1.0.0-M1</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock/
implementation 'org.expedientframework.facilemock:facile-mock:1.0.0-M1'
// https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock/
implementation ("org.expedientframework.facilemock:facile-mock:1.0.0-M1")
'org.expedientframework.facilemock:facile-mock:pom:1.0.0-M1'
<dependency org="org.expedientframework.facilemock" name="facile-mock" rev="1.0.0-M1">
  <artifact name="facile-mock" type="pom" />
</dependency>
@Grapes(
@Grab(group='org.expedientframework.facilemock', module='facile-mock', version='1.0.0-M1')
)
libraryDependencies += "org.expedientframework.facilemock" % "facile-mock" % "1.0.0-M1"
[org.expedientframework.facilemock/facile-mock "1.0.0-M1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-core jar 2.11.1
org.apache.logging.log4j : log4j-slf4j-impl jar 2.11.1

test (1)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.11.1

Project Modules

  • facile-mock-core
  • facile-mock-http-proxy

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);
    }
}

Versions

Version
1.0.0-M1