https://github.com/infinum/retromock
Library for mocking responses in a Retrofit service.
https://github.com/infinum/retromock
android android-development android-library java-library kotlin kotlin-android mock mocking open-source retrofit testing testing-tools
Last synced: 10 months ago
JSON representation
Library for mocking responses in a Retrofit service.
- Host: GitHub
- URL: https://github.com/infinum/retromock
- Owner: infinum
- License: apache-2.0
- Created: 2018-10-05T09:24:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-07-08T15:39:52.000Z (12 months ago)
- Last Synced: 2025-08-09T03:54:30.542Z (11 months ago)
- Topics: android, android-development, android-library, java-library, kotlin, kotlin-android, mock, mocking, open-source, retrofit, testing, testing-tools
- Language: Kotlin
- Homepage:
- Size: 332 KB
- Stars: 68
- Watchers: 15
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Retromock
[](https://app.bitrise.io/app/7b832efc5bb97051)
[  ](https://search.maven.org/artifact/co.infinum/retromock)

## Description
Adapts Java interface created by [Retrofit][retrofit] using annotations on declared methods to define response mocks.
## Table of contents
* [Requirements](#requirements)
* [Getting started](#getting-started)
* [Usage](#usage)
* [Contributing](#contributing)
* [License](#license)
* [Credits](#credits)
## Requirements
You need to use [Retrofit][retrofit] in your project
## Getting started
To include _Retromock_ in your project, you have to add buildscript dependencies in your project
level `build.gradle` or `build.gradle.kts`:
**Groovy**
```groovy
buildscript {
repositories {
mavenCentral()
}
}
```
**KotlinDSL**
```kotlin
buildscript {
repositories {
mavenCentral()
}
}
```
Then, you can include the library in your module's `build.gradle` or `build.gradle.kts`:
**Groovy**
```groovy
implementation 'com.infinum:retromock:1.2.1'
```
**KotlinDSL**
```kotlin
implementation("com.infinum:retromock:1.2.1")
```
## Usage
#### Initialize
```java
Retromock retromock = new Retromock.Builder()
.retrofit(retrofit)
.build();
```
#### Create a service class
```java
Service service = retromock.create(Service.class);
```
#### Setup mocks
```java
public interface Service {
@Mock
@MockResponse(body = "{\"name\":\"John\", \"surname\":\"Doe\"}")
@GET("/endpoint")
Call getUser();
}
```
#### Use the service
```java
Call = 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.
```java
Retromock retromock = new Retromock.Builder()
.retrofit(retrofit)
.defaultBodyFactory(...)
.build();
```
```java
public interface Service {
@Mock
@MockResponse(body = "response.json")
@GET("/endpoint")
Call getUser();
}
```
##### Load responses from Android assets
```java
Retromock retromock = new Retromock.Builder()
.retrofit(retrofit)
.defaultBodyFactory(context.getAssets()::open)
.build();
```
```java
public interface Service {
@Mock
@MockResponse(body = "retromock/response.json")
@GET("/endpoint")
Call 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:
```groovy
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:
```java
Retromock retromock = new Retromock.Builder()
.retrofit(retrofit)
.defaultBodyFactory(new NonEmptyBodyFactory(...))
.build();
```
#### For more information please see [the full specification][specification].
#### ProGuard
The library does not require any ProGuard rules.
However, you might need rules for Retrofit and its dependencies.
## Contributing
We believe that the community can help us improve and build better a product.
Please refer to our [contributing guide](CONTRIBUTING.md) to learn about the types of contributions we accept and the process for submitting them.
To ensure that our community remains respectful and professional, we defined a [code of conduct](CODE_OF_CONDUCT.md) that we expect all contributors to follow.
For easier developing a `samples` with proper implementations are provided.
We appreciate your interest and look forward to your contributions.
## 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](http://www.infinum.com).
[retrofit]: https://square.github.io/retrofit/
[specification]: SPECIFICATION.md