Example Application

A simpler way to prepare your responses with okhttp/mockwebserver, betamax style

License

License

GroupId

GroupId

com.github.rodrigosaito
ArtifactId

ArtifactId

mockwebserver-player
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Example Application
A simpler way to prepare your responses with okhttp/mockwebserver, betamax style
Project URL

Project URL

https://github.com/rodrigosaito/mockwebserver-player
Source Code Management

Source Code Management

https://github.com/rodrigosaito/mockwebserver-player

Download mockwebserver-player

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.rodrigosaito/mockwebserver-player/ -->
<dependency>
    <groupId>com.github.rodrigosaito</groupId>
    <artifactId>mockwebserver-player</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.rodrigosaito/mockwebserver-player/
implementation 'com.github.rodrigosaito:mockwebserver-player:1.1.0'
// https://jarcasting.com/artifacts/com.github.rodrigosaito/mockwebserver-player/
implementation ("com.github.rodrigosaito:mockwebserver-player:1.1.0")
'com.github.rodrigosaito:mockwebserver-player:jar:1.1.0'
<dependency org="com.github.rodrigosaito" name="mockwebserver-player" rev="1.1.0">
  <artifact name="mockwebserver-player" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.rodrigosaito', module='mockwebserver-player', version='1.1.0')
)
libraryDependencies += "com.github.rodrigosaito" % "mockwebserver-player" % "1.1.0"
[com.github.rodrigosaito/mockwebserver-player "1.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
junit : junit jar 4.11
com.squareup.okhttp : mockwebserver jar 2.2.0
org.yaml : snakeyaml jar 1.14

test (1)

Group / Artifact Type Version
org.apache.httpcomponents : httpclient jar 4.3.6

Project Modules

There are no modules declared in this project.

Mockwebserver Player Circle CI

Mockwebserver Player is a tool to simplify the use of mockwebserver in your JUnit tests

Motivation

When returning large responses such as JSON/XML it could be really boring to write all the code to prepare your MockResponses this project makes it easier to write your tests.

Usage

Installation

Add dependency to your pom.xml.

<dependency>
  <groupId>com.github.rodrigosaito</groupId>
  <artifactId>mockwebserver-player</artifactId>
  <version>1.1.0</version>
</dependency>

Simple response

Add a @Rule Player property to your test class.

Annotate your test methods with @Play("simple_play") where simple_play is the name of the file containing the MockResponse attributes.

@Rule
public Player player = new Player();

@Test
@Play("simple_play")
public void testPlayerWithDefaultSettings() throws IOException {
    URL url = player.getURL("/");

    // Execute your application code that makes the request to mockwebserver
    CloseableHttpResponse response = executeGet(url.toString());
    String body = EntityUtils.toString(response.getEntity());
    int statusCode = response.getStatusLine().getStatusCode();

    assertEquals("Simple Tape", body);
    assertEquals(200, statusCode);
}

Create a plays/simple_play.yaml file inside your test directory.

!play
name: simple play
interactions:
- response:
    status: 200
    headers:
    body: Simple Tape

Request Matching

Sometime you would like to match a request with a response in this case we have the following tape that will matches the request with path /test_1 with the response containing body Test 1 and request with path /test_2 with the response containing body Test 2.

If you add headers or body to request it will use them on matching as well.

!play
name: play with request matching
interactions:
- request:
    method: GET
    uri: /test_1
    headers:
      Host: localhost
  response:
    status: 200
    headers:
    body: Test 1
- request:
    method: GET
    uri: /test_2
    headers:
      Host: localhost
  response:
    status: 200
    body: Test 2

And the test.

@Test
@Play("play_with_request_matching")
public void test() throws IOException {
    // Execute request that matches with the second request on tape
    URL url2 = player.getURL("/test_2");
    CloseableHttpResponse response2 = executeGet(url2.toString());
    assertEquals("Test 2", EntityUtils.toString(response2.getEntity()));

    // Execute request that matches with the first request on tape
    URL url1 = player.getURL("/test_1");
    CloseableHttpResponse response1 = executeGet(url1.toString());
    assertEquals("Test 1", EntityUtils.toString(response1.getEntity()));
}

Custom Settings

player.setPort(58880); // changes the port of mockwebserver default is a random port

Versions

Version
1.1.0
1.0.0