8

I am very new to testing and TDD and I decided to use use Retrofit2-Mock for my api mocking needs. The documentation on Mock Retrofit2 is virtually non existent and the only how-to resources that I found is this article from 2015 and this answer from 2016.

In these a BehaviorDelegate class is used which does not implement the mocked api Interface and needs to be wrapped.

Is there a more elegant way to obtain the mock api service?

Or am I missing the whole point and the Retrofit2-Mock tool is not considered to be in the "best practice stack"? Specially since there are so few articles about it

saiedmomen
  • 1,401
  • 16
  • 33

3 Answers3

9

This issue on Retrofit's Github repo is asking about the non-existent documentation you were asking about (it is still open while writing this answer).

Well, you have 2 options (both are in the article you already mentioned), and it depends on how you want to define your Givens/Inputs:

Option 1: (Okhttp's MockWebServer)

If you usually start your TDD by dealing with your backend's json response (using something like Postman), & you would feel more confident if you used that returned json directly as the input for your tests, then use MockWebServer, where you would copy/paste the json you already have & start developing your tests from there.

Option 2: (Retrofit's own Mock Web Server)

If you prefer defining your givens/inputs using objects for the models you already use in your code, which would make your tests more readable & controllable, then use Retrofit's mock web server just like how it is used in this official sample mentioned by @JakeWharton


Both options are developed/maintained by the same awesome people of Square, so it is really about how you want to define your givens/inputs.

AbdelHady
  • 9,334
  • 8
  • 56
  • 83
8

I usually use Mockito like this

  1. Import Retrofit Mock

    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit-mock</artifactId>
        <version>${version.retrofit}</version>
        <scope>test</scope>
    </dependency>
    
  2. Create and use the mock

    import retrofit2.mock.Calls;
    import static org.mockito.Mockito.when;
    import static org.mockito.Mockito.mock;
    
    ...
    
    Api api = mock(Api.class); // Mockito mock
    
    ...
    
    when(api.doSomething(param)).thenReturn(Calls.response(response));
    

Retrofit Mock is used only to generate the response.

Lukas
  • 13,606
  • 9
  • 31
  • 40
0

Well I thing this article could be useful in this case.

The whole idea is based on build variant you can switch between mock up server on real one.

Annotation, Call Adapter and Interception

This is how annotation will look like:

    @Target(
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER
)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class MOCKUP(vararg val value: String)

use this annotation in APIs before or after @GET @POST ...etc

For the full example here

MusabAlothman
  • 68
  • 1
  • 8