MockRetrofit and BehaviorDelegate ignore OkHttpClient

I am writing an SDK for our MBaaS service. I am using Retrofit2 for my REST calls.

I have an Interceptor (very similar to this ) for updating an expired token, which makes rather complicated if-else cases. To test the interceptor, I need to make fun of our API, which I use with the MockRetrofit library. So far, so good! (See End of Question for some info on my interceptor class)

Here is the code:

 @Before public void setup() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(BacktoryClient.BASE_URL) .client(new OkHttpClient.Builder().addInterceptor(new RetryWithRefreshedTokenInterceptor()).build()) .build(); NetworkBehavior behavior = NetworkBehavior.create(); MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); delegate = mockRetrofit.create(AuthApiDefinition.class); } @Test public void testRetryWithRefreshedToken() throws Exception { SimpleMock apiMock = new SimpleMock(delegate); Response<SomeApiResponse> response = apiMock.someApiCall(<parameters>).execute(); // assert part } 

The problem is that the response object in the test method is what it should be (indicating that the mocking part is ok), but the RetryWithRefreshedTokenInterceptor interceptor is not called. Therefore, I assume that MockRetrofit ignores OkHttpClient .

Now, asking the real question, is this some kind of mistake, or did I completely misunderstand the use of the MockRetrofit library? If it was you, how did you test the interceptor?


Additional information about RetryWithRefreshedTokenInterceptor: checks if the response code is 401 or not, and if so it calls the method of our SDK, which itself calls the method of our API (here from the manufactured API) to get a new token and stores it in persistent storage (amount of logic handled in the API methods used in the interceptor is large enough, I cannot isolate the interceptor to test it with MockWebserver )

+5
source share

All Articles