Java Code Examples for retrofit2.mock.NetworkBehavior#create()

The following examples show how to use retrofit2.mock.NetworkBehavior#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiModule.java    From AndroidBlueprints with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
MockRetrofit provideRetrofit(GsonConverterFactory gson, OkHttpClient okHttpClient) {
    NetworkBehavior behavior = NetworkBehavior.create();
    behavior.setDelay(1, TimeUnit.SECONDS);
    behavior.setVariancePercent(40);
    behavior.setErrorPercent(2);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory.create())
            .client(okHttpClient)
            .build();
    return new MockRetrofit.Builder(retrofit)
            .networkBehavior(behavior)
            .build();
}
 
Example 2
Source File: LyftButtonCallManagerTest.java    From lyft-android-sdk with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(LyftApi.API_ROOT)
            .build();

    NetworkBehavior behavior = NetworkBehavior.create();
    behavior.setDelay(1, TimeUnit.MILLISECONDS);
    behavior.setVariancePercent(0);
    behavior.setFailurePercent(0);
    MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
            .networkBehavior(behavior)
            .build();
    BehaviorDelegate<LyftApi> lyftApidelegate = mockRetrofit.create(LyftApi.class);
    lyftPublicApi = spy(new MockLyftApi(lyftApidelegate));

    BehaviorDelegate<GoogleApi> googleApiDelegate = mockRetrofit.create(GoogleApi.class);
    googleApi = spy(new MockGoogleApi(googleApiDelegate));
    HashMap<String, GoogleLatLng> addressToLatLngMap = new HashMap<>();
    addressToLatLngMap.put(PICKUP_ADDR, new GoogleLatLng(PICKUP_LAT, PICKUP_LNG));
    addressToLatLngMap.put(DROPOFF_ADDR, new GoogleLatLng(DROPOFF_LAT, DROPOFF_LNG));
    googleApi.setAddressToLatLngMap(addressToLatLngMap);

    mockCallback = mock(LyftButton.ResultLoadedCallback.class);
    callManager = new LyftButtonCallManager("clientId", lyftPublicApi, googleApi, new MockExecutorService());
}
 
Example 3
Source File: RxRetrofitInterceptor.java    From Mockery with Apache License 2.0 5 votes vote down vote up
private NetworkBehavior networkBehaviour(Metadata<RxRetrofit> metadata) {
  RxRetrofit retrofit = metadata.getAnnotation();

  NetworkBehavior networkBehavior = NetworkBehavior.create();
  networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS);
  networkBehavior.setVariancePercent(retrofit.variancePercentage());
  networkBehavior.setFailurePercent(retrofit.failurePercent());

  return networkBehavior;
}
 
Example 4
Source File: Rx2RetrofitInterceptor.java    From Mockery with Apache License 2.0 5 votes vote down vote up
private NetworkBehavior networkBehaviour(Metadata<Rx2Retrofit> metadata) {
  Rx2Retrofit retrofit = metadata.getAnnotation();

  NetworkBehavior networkBehavior = NetworkBehavior.create();
  networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS);
  networkBehavior.setVariancePercent(retrofit.variancePercentage());
  networkBehavior.setFailurePercent(retrofit.failurePercent());

  return networkBehavior;
}
 
Example 5
Source File: RetrofitInterceptor.java    From Mockery with Apache License 2.0 5 votes vote down vote up
private NetworkBehavior networkBehaviour(Metadata<Retrofit> metadata) {
  Retrofit retrofit = metadata.getAnnotation();

  NetworkBehavior networkBehavior = NetworkBehavior.create();
  networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS);
  networkBehavior.setVariancePercent(retrofit.variancePercentage());
  networkBehavior.setFailurePercent(retrofit.failurePercent());

  return networkBehavior;
}
 
Example 6
Source File: Helpers.java    From quill with MIT License 5 votes vote down vote up
private static NetworkBehavior getNetworkBehavior(int delayMsec, int delayVariance,
                                                  int failurePercent, int errorPercent) {
    NetworkBehavior networkBehavior = NetworkBehavior.create();
    networkBehavior.setDelay(delayMsec, TimeUnit.MILLISECONDS);
    networkBehavior.setVariancePercent(delayVariance);
    // "failure" means network layer failure
    networkBehavior.setFailurePercent(failurePercent);
    // "error" means HTTP error
    networkBehavior.setErrorPercent(errorPercent);
    return networkBehavior;
}
 
Example 7
Source File: QuoteOfTheDayMockAdapterTest.java    From android-retrofit-test-examples with MIT License 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();
    retrofit = new Retrofit.Builder().baseUrl("http://test.com")
            .client(new OkHttpClient())
            .addConverterFactory(JacksonConverterFactory.create())
            .build();

    NetworkBehavior behavior = NetworkBehavior.create();

    mockRetrofit = new MockRetrofit.Builder(retrofit)
            .networkBehavior(behavior)
            .build();
}
 
Example 8
Source File: DebugApiModule.java    From u2020-mvp with Apache License 2.0 5 votes vote down vote up
@Provides
@ApplicationScope
NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay,
                                @NetworkFailurePercent Preference<Integer> networkFailurePercent,
                                @NetworkVariancePercent Preference<Integer> networkVariancePercent) {
    NetworkBehavior behavior = NetworkBehavior.create();
    behavior.setDelay(networkDelay.get(), MILLISECONDS);
    behavior.setFailurePercent(networkFailurePercent.get());
    behavior.setVariancePercent(networkVariancePercent.get());
    return behavior;
}
 
Example 9
Source File: DebugApiModule.java    From u2020 with Apache License 2.0 5 votes vote down vote up
@Provides @Singleton NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay,
    @NetworkVariancePercent Preference<Integer> networkVariancePercent,
    @NetworkFailurePercent Preference<Integer> networkFailurePercent,
    @NetworkErrorPercent Preference<Integer> networkErrorPercent,
    Preference<NetworkErrorCode> networkErrorCode) {
  NetworkBehavior behavior = NetworkBehavior.create();
  behavior.setDelay(networkDelay.get(), MILLISECONDS);
  behavior.setVariancePercent(networkVariancePercent.get());
  behavior.setFailurePercent(networkFailurePercent.get());
  behavior.setErrorPercent(networkErrorPercent.get());
  behavior.setErrorFactory(
      () -> Response.error(networkErrorCode.get().code, ResponseBody.create(null, new byte[0])));
  return behavior;
}