本文整理汇总了Java中retrofit2.mock.Calls类的典型用法代码示例。如果您正苦于以下问题:Java Calls类的具体用法?Java Calls怎么用?Java Calls使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Calls类属于retrofit2.mock包,在下文中一共展示了Calls类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExecution
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testExecution() throws Exception {
ApiPendingResult<Applet> pendingResult = new ApiPendingResult<>(Calls.<Applet>failure(new IOException()),
new Moshi.Builder().build().adapter(ErrorResponse.class));
final AtomicReference<ErrorResponse> errorResponseAtomicReference = new AtomicReference<>();
pendingResult.execute(new PendingResult.ResultCallback<Applet>() {
@Override
public void onSuccess(@NonNull Applet result) {
fail();
}
@Override
public void onFailure(@NonNull ErrorResponse errorResponse) {
errorResponseAtomicReference.set(errorResponse);
}
});
assertThat(errorResponseAtomicReference.get().code).isEqualTo("exception");
assertThat(errorResponseAtomicReference.get().message).isEqualTo("Unexpected error");
}
示例2: testStoreExternal
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testStoreExternal() throws Exception {
String jsonString = "{'url': 'https://cdn.filestackcontent.com/handle'}";
Gson gson = new Gson();
StoreResponse storeResponse = gson.fromJson(jsonString, StoreResponse.class);
String url = "https://example.com/image.jpg";
Mockito
.doReturn(Calls.response(storeResponse))
.when(Networking.getCdnService())
.transformStoreExt("apiKey", "store", url);
Config config = new Config("apiKey");
ImageTransform transform = new ImageTransform(config, url, true);
Assert.assertNotNull(transform.store());
}
示例3: setupStartMock
import retrofit2.mock.Calls; //导入依赖的package包/类
private static void setupStartMock(UploadService mockUploadService) {
String jsonString = "{"
+ "'uri' : '/bucket/apikey/filename',"
+ "'region' : 'region',"
+ "'upload_id' : 'id',"
+ "'location_url' : 'url',"
+ "'upload_type' : 'intelligent_ingestion'"
+ "}";
Gson gson = new Gson();
StartResponse response = gson.fromJson(jsonString, StartResponse.class);
Call call = Calls.response(response);
Mockito
.doReturn(call)
.when(mockUploadService)
.start(Mockito.<String, RequestBody>anyMap());
}
示例4: setupUploadMock
import retrofit2.mock.Calls; //导入依赖的package包/类
private static void setupUploadMock(UploadService mockUploadService) {
String jsonString = "{"
+ "'url' : 'https://s3.amazonaws.com/path',"
+ "'headers' : {"
+ "'Authorization' : 'auth_value',"
+ "'Content-MD5' : 'md5_value',"
+ "'x-amz-content-sha256' : 'sha256_value',"
+ "'x-amz-date' : 'date_value',"
+ "'x-amz-acl' : 'acl_value'"
+ "},"
+ "'location_url' : 'url'"
+ "}";
Gson gson = new Gson();
final UploadResponse response = gson.fromJson(jsonString, UploadResponse.class);
Mockito
.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return Calls.response(response);
}
})
.when(mockUploadService)
.upload(Mockito.<String, RequestBody>anyMap());
}
示例5: setupUploadS3Mock
import retrofit2.mock.Calls; //导入依赖的package包/类
private static void setupUploadS3Mock(UploadService mockUploadService) {
MediaType mediaType = MediaType.parse("text/xml");
ResponseBody responseBody = ResponseBody.create(mediaType, "");
final Response<ResponseBody> response = Response.success(responseBody,
Headers.of("ETag", "test-etag"));
Mockito
.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return Calls.response(response);
}
})
.when(mockUploadService)
.uploadS3(Mockito.<String, String>anyMap(), Mockito.anyString(),
Mockito.any(RequestBody.class));
}
示例6: setupCompleteMock
import retrofit2.mock.Calls; //导入依赖的package包/类
private static void setupCompleteMock(UploadService mockUploadService) {
String jsonString = "{"
+ "'handle' : 'handle',"
+ "'url' : 'url',"
+ "'filename' : 'filename',"
+ "'size' : '0',"
+ "'mimetype' : 'mimetype'"
+ "}";
Gson gson = new Gson();
CompleteResponse response = gson.fromJson(jsonString, CompleteResponse.class);
Call call = Calls.response(response);
Mockito
.doReturn(call)
.when(mockUploadService)
.complete(Mockito.<String, RequestBody>anyMap());
}
示例7: testImageSfw
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testImageSfw() throws Exception {
Mockito.doAnswer(new Answer() {
@Override
public Call<ResponseBody> answer(InvocationOnMock invocation) throws Throwable {
String handle = invocation.getArgument(1);
String json = "{'sfw': " + (handle.equals("safe") ? "true" : "false") + "}";
MediaType mediaType = MediaType.parse("application/json");
return Calls.response(ResponseBody.create(mediaType, json));
}
})
.when(Networking.getCdnService())
.transform(Mockito.anyString(), Mockito.anyString());
Config config = new Config("apiKey", "policy", "signature");
FileLink safe = new FileLink(config, "safe");
FileLink notSafe = new FileLink(config, "notSafe");
Assert.assertTrue(safe.imageSfw());
Assert.assertFalse(notSafe.imageSfw());
}
示例8: invokesUploadErrorCallback
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test public void invokesUploadErrorCallback() throws Exception {
transporter = new Transporter(queue, trackingAPI, callback);
Message message = new TrackMessage.Builder().userId("userId").event("event").build();
IOException testException = new IOException("test");
Call<Void> call = Calls.failure(testException);
when(trackingAPI.batch(any(Batch.class))).thenReturn(call);
transporter.enqueue(message).get();
try {
transporter.flush().get();
} catch (ExecutionException e) {
assertThat(e.getCause()).isEqualTo(testException);
}
verify(callback).error(Callback.Event.UPLOAD, message, testException);
}
示例9: refreshAuthToken
import retrofit2.mock.Calls; //导入依赖的package包/类
@Override
public Observable<AuthToken> refreshAuthToken(@Body RefreshReqBody credentials) {
if (credentials.refreshToken.equals("refresh-token")) {
AuthToken token = new AuthToken();
token.setAccessToken("refreshed-access-token");
// token.setRefreshToken("refresh-token"); // refreshed tokens don't have a new refresh token
token.setCreatedAt(System.currentTimeMillis());
token.setExpiresIn(60 * 1000);
return mDelegate
.returningResponse(token)
.refreshAuthToken(credentials);
} else {
// expired / invalid refresh token
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"),
"{\"errors\":[{\"message\":\"Expired or invalid refresh token\",\"errorType\":\"UnauthorizedError\"}]}");
Response<Object> res = Response.error(401, body);
return mDelegate
.returning(Calls.response(res))
.refreshAuthToken(credentials);
}
}
示例10: testRequestGuestAuthToken_guestFailure
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testRequestGuestAuthToken_guestFailure() {
service.api = new MockOAuth2Api() {
@Override
public Call<GuestTokenResponse> getGuestToken(@Header(OAuthConstants.HEADER_AUTHORIZATION) String auth) {
return Calls.failure(new IOException());
}
};
service.requestGuestAuthToken(new Callback<GuestAuthToken>() {
@Override
public void success(Result<GuestAuthToken> result) {
fail();
}
@Override
public void failure(TwitterException error) {
assertNotNull(error);
}
});
}
示例11: testRequestGuestAuthToken_appAuthFailure
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testRequestGuestAuthToken_appAuthFailure() {
service.api = new MockOAuth2Api() {
@Override
public Call<OAuth2Token> getAppAuthToken(@Header(OAuthConstants.HEADER_AUTHORIZATION) String auth,
@Field(OAuthConstants.PARAM_GRANT_TYPE) String grantType) {
return Calls.failure(new IOException());
}
};
service.requestGuestAuthToken(new Callback<GuestAuthToken>() {
@Override
public void success(Result<GuestAuthToken> result) {
fail();
}
@Override
public void failure(TwitterException error) {
assertNotNull(error);
}
});
}
示例12: testRequestEmail_withSuccess
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testRequestEmail_withSuccess() {
final User user = new UserBuilder().setEmail(TEST_EMAIL).build();
final Call<User> call = Calls.response(user);
setupMockAccountService(call);
authClient.requestEmail(mock(TwitterSession.class), new Callback<String>() {
@Override
public void success(Result<String> result) {
assertEquals(TEST_EMAIL, result.data);
}
@Override
public void failure(TwitterException exception) {
fail("Expected Callback#success to be called");
}
});
}
示例13: testRequestEmail_withFailure
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testRequestEmail_withFailure() {
final IOException networkException = new IOException("Network failure");
final Call<User> call = Calls.failure(networkException);
setupMockAccountService(call);
authClient.requestEmail(mock(TwitterSession.class), new Callback<String>() {
@Override
public void success(Result<String> result) {
fail("Expected Callback#failure to be called");
}
@Override
public void failure(TwitterException exception) {
assertEquals(exception.getCause(), networkException);
}
});
}
示例14: testRequestEmail_scribesImpression
import retrofit2.mock.Calls; //导入依赖的package包/类
@Test
public void testRequestEmail_scribesImpression() {
final IOException networkException = new IOException("Network failure");
final Call<User> call = Calls.failure(networkException);
setupMockAccountService(call);
final TwitterSession mockSession = mock(TwitterSession.class);
when(mockSession.getId()).thenReturn(TestFixtures.USER_ID);
authClient = new TwitterAuthClient(mockTwitterCore, mockAuthConfig, mockSessionManager,
mockAuthState) {
@Override
protected DefaultScribeClient getScribeClient() {
return mockScribeClient;
}
};
authClient.requestEmail(mockSession, mock(Callback.class));
verify(mockScribeClient).scribe(any(EventNamespace.class));
}
示例15: setUp
import retrofit2.mock.Calls; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
context = RuntimeEnvironment.application;
mockMediaService = mock(MediaService.class);
mockStatusesService = mock(StatusesService.class);
tweet = new TweetBuilder().setId(123L).setText(EXPECTED_TWEET_TEXT).build();
when(mockMediaService
.upload(any(RequestBody.class), any(RequestBody.class), any(RequestBody.class)))
.thenReturn(mock(Call.class));
when(mockStatusesService.update(anyString(), isNull(Long.class), isNull(Boolean.class),
isNull(Double.class), isNull(Double.class), isNull(String.class),
isNull(Boolean.class), eq(true), isNull(String.class)))
.thenReturn(Calls.response(tweet));
mockTwitterApiClient = mock(TwitterApiClient.class);
when(mockTwitterApiClient.getStatusesService()).thenReturn(mockStatusesService);
when(mockTwitterApiClient.getMediaService()).thenReturn(mockMediaService);
mockDependencyProvider = mock(TweetUploadService.DependencyProvider.class);
when(mockDependencyProvider.getTwitterApiClient(any(TwitterSession.class)))
.thenReturn(mockTwitterApiClient);
service = spy(Robolectric.buildService(TweetUploadService.class).create().get());
service.dependencyProvider = mockDependencyProvider;
}