Rakesh
Rakesh

Reputation: 4127

MockRetrofit doesn't allow to avoid network error

I using MockRetrofit(retrofit 2 snapshot 4) to mock the server apis.

NetworkBehavior networkBehavior = NetworkBehavior.create();

networkBehavior.setFailurePercent(1);
networkBehavior.setDelay(500, TimeUnit.MILLISECONDS);

MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
    .networkBehavior(networkBehavior)
    .build();

BehaviorDelegate<FinderCommonApis> delegate = mockRetrofit.create(FinderCommonApis.class);
finderCommonApis = new MockFinderCommonApis(delegate);

I am also using this mock in my test cases. My test cases intermittently fail because of the NetworkBehavior. It introduces the network error which causes the intermittent failure. I want to completely avoid this network error, but it doesn't look like I can completely avoid it. I even tried to set the failure percentage to 0 but it doesn't allow. So I set the lowest possible value that is 1. They have made NetworkBehavior optional in MockRetrofit.Builder but it provides its own default NetworkBehavior which also introduces same network error. I checked the NetworkBehavior code and they have made it final class so I can't override it. I am running out of idea to avoid this problem.

I just wanted to know how to completely avoid the NetworkBehavior.

Upvotes: 1

Views: 476

Answers (1)

Jake Wharton
Jake Wharton

Reputation: 76075

I even tried to set the failure percentage to 0 but it doesn't allow.

This is not true. See: https://github.com/square/retrofit/blob/78897005be619c3b63d238bf5d0de0f1580d95d4/retrofit-mock/src/main/java/retrofit2/mock/NetworkBehavior.java#L106-L109

0 is a valid value and is what should be used to disable all failure.

Upvotes: 2

Related Questions