本文整理汇总了Java中retrofit2.mock.NetworkBehavior类的典型用法代码示例。如果您正苦于以下问题:Java NetworkBehavior类的具体用法?Java NetworkBehavior怎么用?Java NetworkBehavior使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NetworkBehavior类属于retrofit2.mock包,在下文中一共展示了NetworkBehavior类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: provideRetrofit
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@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();
}
示例2: adapt
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
<T> T adapt(Method method, NetworkBehavior networkBehavior, Call<?> call) {
try {
Class<?> behaviorCallClass = Class.forName("retrofit2.mock.BehaviorCall");
Constructor<?> constructor = behaviorCallClass.getDeclaredConstructor(NetworkBehavior.class,
ExecutorService.class, Call.class);
constructor.setAccessible(true);
Call<?> behaviorCall = (Call<?>) constructor.newInstance(networkBehavior,
Executors.newCachedThreadPool(),
call);
return (T) retrofit
.callAdapter(method.getGenericReturnType(), method.getAnnotations())
.adapt(behaviorCall);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例3: provideBusService
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
public static BusService provideBusService(Context context){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
} else {
interceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
}
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BusService.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
NetworkBehavior behavior = NetworkBehavior.create();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
.networkBehavior(behavior)
.build();
behavior.failureException();
BehaviorDelegate<BusService> delegate = mockRetrofit.create(BusService.class);
return new MockBusService(context,delegate);
}
示例4: main
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
public static void main(String... args) throws IOException {
// Create a very simple Retrofit adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SimpleService.API_URL)
.build();
// Create a MockRetrofit object with a NetworkBehavior which manages the fake behavior of calls.
NetworkBehavior behavior = NetworkBehavior.create();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
.networkBehavior(behavior)
.build();
BehaviorDelegate<GitHub> delegate = mockRetrofit.create(GitHub.class);
MockGitHub gitHub = new MockGitHub(delegate);
// Query for some contributors for a few repositories.
printContributors(gitHub, "square", "retrofit");
printContributors(gitHub, "square", "picasso");
// Using the mock-only methods, add some additional data.
System.out.println("Adding more mock data...\n");
gitHub.addContributor("square", "retrofit", "Foo Bar", 61);
gitHub.addContributor("square", "picasso", "Kit Kat", 53);
// Reduce the delay to make the next calls complete faster.
behavior.setDelay(500, TimeUnit.MILLISECONDS);
// Query for the contributors again so we can see the mock data that was added.
printContributors(gitHub, "square", "retrofit");
printContributors(gitHub, "square", "picasso");
}
示例5: provideBehavior
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Provides @Singleton 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;
}
示例6: provideNetworkBehavior
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Provides
public NetworkBehavior provideNetworkBehavior() {
final NetworkBehavior networkBehavior = NetworkBehavior.create();
networkBehavior.setDelay(2, TimeUnit.SECONDS);
networkBehavior.setVariancePercent(20);
networkBehavior.setFailurePercent(0);
return networkBehavior;
}
示例7: provideNetworkRetrofit
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Provides
@Singleton
public MockRetrofit provideNetworkRetrofit(Retrofit retrofit, NetworkBehavior networkBehavior) {
final MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
.networkBehavior(networkBehavior).build();
return mockRetrofit;
}
示例8: MockMessageRepository
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
public MockMessageRepository(Retrofit retrofit, MockRetrofit mockRetrofit,
NetworkBehavior networkBehavior) {
this.networkBehavior = networkBehavior;
final BehaviorDelegate<MessageApiService> delegate = mockRetrofit.create(MessageApiService.class);
mockMessageApiService = new MockMessageApiService(delegate);
}
示例9: setup
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Before
public void setup() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(LyftPublicApi.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<LyftPublicApi> lyftApidelegate = mockRetrofit.create(LyftPublicApi.class);
lyftPublicApi = spy(new MockLyftPublicApi(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());
}
示例10: onLegalMock
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Override public Observable onLegalMock(final Object mock, final Metadata<RxRetrofit> metadata) {
checkReturnMethodTypeIsObservable(metadata);
checkTypeMockIsNotObservableNeitherResponse(metadata, mock);
NetworkBehavior networkBehavior = networkBehaviour(metadata);
return callAdapter.adapt(metadata.getMethod(),
networkBehavior, Calls.response(mock));
}
示例11: onIllegalMock
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Override public Observable onIllegalMock(final AssertionError assertionError, final Metadata<RxRetrofit> metadata) {
checkReturnMethodTypeIsObservable(metadata);
final String errorMessage = assertionError.getMessage() != null ? assertionError.getMessage() : "";
final String adaptedErrorMessage = adaptErrorResponse(errorMessage, metadata);
NetworkBehavior networkBehavior = networkBehaviour(metadata);
final ResponseBody responseBody = ResponseBody
.create(MediaType.parse("application/json"), adaptedErrorMessage);
return callAdapter.adapt(metadata.getMethod(),
networkBehavior, Calls.response(Response.error(404, responseBody)));
}
示例12: networkBehaviour
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
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;
}
示例13: onLegalMock
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Override public Object onLegalMock(final Object mock, final Metadata<Rx2Retrofit> metadata) {
checkReturnMethodTypeIsSingleOrCompletable(metadata);
checkTypeMockIsNotSingleNeitherResponse(metadata, mock);
NetworkBehavior networkBehavior = networkBehaviour(metadata);
return callAdapter.adapt(metadata.getMethod(),
networkBehavior, Calls.response(mock));
}
示例14: onIllegalMock
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
@Override public Object onIllegalMock(final AssertionError assertionError, final Metadata<Rx2Retrofit> metadata) {
checkReturnMethodTypeIsSingleOrCompletable(metadata);
final String errorMessage = assertionError.getMessage() != null ? assertionError.getMessage() : "";
final String adaptedErrorMessage = adaptErrorResponse(errorMessage, metadata);
NetworkBehavior networkBehavior = networkBehaviour(metadata);
final ResponseBody responseBody = ResponseBody
.create(MediaType.parse("application/json"), adaptedErrorMessage);
return callAdapter.adapt(metadata.getMethod(),
networkBehavior, Calls.response(Response.error(404, responseBody)));
}
示例15: networkBehaviour
import retrofit2.mock.NetworkBehavior; //导入依赖的package包/类
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;
}