All Projects → infinum → Retromock

infinum / Retromock

Licence: Apache-2.0 License
Java library for mocking responses in a Retrofit service.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Retromock

Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+3329.17%)
Mutual labels:  mock, mocking, testing-tools
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+106.25%)
Mutual labels:  mock, mocking, testing-tools
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+272.92%)
Mutual labels:  mock, mocking, testing-tools
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (+120.83%)
Mutual labels:  mock, mocking, testing-tools
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+3920.83%)
Mutual labels:  mock, mocking, testing-tools
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+25843.75%)
Mutual labels:  mock, mocking, testing-tools
Okhttp Json Mock
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Stars: ✭ 231 (+381.25%)
Mutual labels:  mock, retrofit, mocking
MockAlamofire
A simple example showing how to override the URLProtocol to return mock data on Alamofire responses. Helpful if you are looking for a simple way to mock an Alamofire response, with out any additional dependencies.
Stars: ✭ 22 (-54.17%)
Mutual labels:  mock, mocking
main
Mocks Server monorepo
Stars: ✭ 109 (+127.08%)
Mutual labels:  mock, testing-tools
open-api-mocker
A mock server based in OpenAPI Specification
Stars: ✭ 58 (+20.83%)
Mutual labels:  mock, mocking
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (-37.5%)
Mutual labels:  mock, mocking
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (-64.58%)
Mutual labels:  mock, mocking
chrome-extension-mocker
The most convenient tool to mock requests for axios, with built-in Chrome extension support.
Stars: ✭ 37 (-22.92%)
Mutual labels:  mock, mocking
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+114.58%)
Mutual labels:  mock, testing-tools
dubbo-mock
dubbo mock web server
Stars: ✭ 62 (+29.17%)
Mutual labels:  mock, testing-tools
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+60.42%)
Mutual labels:  mock, mocking
dexopener
An Android library that provides the ability to mock your final classes on Android devices.
Stars: ✭ 112 (+133.33%)
Mutual labels:  mock, mocking
Mockaco
🐵 HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting
Stars: ✭ 213 (+343.75%)
Mutual labels:  mock, mocking
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+191.67%)
Mutual labels:  mock, mocking
php-test-generator
Generate test cases for existing PHP files
Stars: ✭ 47 (-2.08%)
Mutual labels:  mocking, testing-tools

Retromock

Build Status Download

Adapts Java interface created by Retrofit using annotations on declared methods to define response mocks.

Quick guide

Add dependency

implementation 'co.infinum:retromock:1.1.1'

Initialize

Retromock retromock = new Retromock.Builder()
  .retrofit(retrofit)
  .build();

Create a service class

Service service = retromock.create(Service.class);

Setup mocks

public interface Service {

  @Mock
  @MockResponse(body = "{\"name\":\"John\", \"surname\":\"Doe\"}")
  @GET("/endpoint")
  Call<User> getUser();
}

Use the service

Call<User> = service.getUser();
Load responses from streams

If you would like to load response from a stream set a default body factory that loads a response stream by a body parameter(response.json) in annotation.

Retromock retromock = new Retromock.Builder()
  .retrofit(retrofit)
  .defaultBodyFactory(...)
  .build();
public interface Service {

  @Mock
  @MockResponse(body = "response.json")
  @GET("/endpoint")
  Call<User> getUser();
}
Load responses from Android assets
Retromock retromock = new Retromock.Builder()
  .retrofit(retrofit)
  .defaultBodyFactory(context.getAssets()::open)
  .build();
public interface Service {

  @Mock
  @MockResponse(body = "retromock/response.json")
  @GET("/endpoint")
  Call<User> getUser();
}

Save a response body content in file named retromock/response.json.

If you use Retromock only in some variants you can exclude files with mock responses from final .apk with configuration similar to:

applicationVariants.all { variant ->
  if (variant.buildType.name.contains('production')) {
    variant.mergeAssets.doLast {
      delete(fileTree(dir: variant.mergeAssets.outputDir, includes: ['**/retromock/*']))
    }
  }
}

Note: if you set custom default body factory and do not declare a bodyFactory parameter in @MockResponse annotation your body factory will be called with value of body parameter. That also applies if you don't specificaly set a body - in that case body is empty by default. If you wouldn't like to handle the case of empty body wrap your default body factory into NonEmptyBodyFactory class as follows:

Retromock retromock = new Retromock.Builder()
  .retrofit(retrofit)
  .defaultBodyFactory(new NonEmptyBodyFactory(...))
  .build();

For more information please see the full specification.


ProGuard

The library does not require any ProGuard rules.

However, you might need rules for Retrofit and its dependencies.

License

Copyright 2019 Infinum

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Credits

Maintained and sponsored by Infinum.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].