Example Application

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

Лицензия

Лицензия

Группа

Группа

com.github.rodrigosaito
Идентификатор

Идентификатор

mockwebserver-player
Последняя версия

Последняя версия

1.1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

Example Application
A simpler way to prepare your responses with okhttp/mockwebserver, betamax style
Ссылка на сайт

Ссылка на сайт

https://github.com/rodrigosaito/mockwebserver-player
Система контроля версий

Система контроля версий

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

Скачать mockwebserver-player

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (3)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.11
com.squareup.okhttp : mockwebserver jar 2.2.0
org.yaml : snakeyaml jar 1.14

test (1)

Идентификатор библиотеки Тип Версия
org.apache.httpcomponents : httpclient jar 4.3.6

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
1.1.0
1.0.0