本文整理了Java中org.powermock.api.mockito.PowerMockito.doAnswer()
方法的一些代码示例,展示了PowerMockito.doAnswer()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PowerMockito.doAnswer()
方法的具体详情如下:
包路径:org.powermock.api.mockito.PowerMockito
类名称:PowerMockito
方法名:doAnswer
[英]Use doAnswer() when you want to stub a void method with generic Answer.
Stubbing voids requires different approach from PowerMockito#when(Object) because the compiler does not like void methods inside brackets...
Example:
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Mock mock = invocation.getMock();
return null;
}
}).when(mock).someMethod();
See examples in javadoc for Mockito class
[中]
代码示例来源:origin: konmik/nucleus
Presenter mockPresenter() {
Presenter presenter = mock(Presenter.class);
final ArrayList<Presenter.OnDestroyListener> onDestroyListeners = new ArrayList<>();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
onDestroyListeners.add((Presenter.OnDestroyListener) invocation.getArguments()[0]);
return null;
}
}).when(presenter).addOnDestroyListener(any(Presenter.OnDestroyListener.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
for (Presenter.OnDestroyListener listener : onDestroyListeners)
listener.onDestroy();
return null;
}
}).when(presenter).destroy();
return presenter;
}
代码示例来源:origin: konmik/nucleus
public PresenterStorage mockStorage() {
PresenterStorage storage = mock(PresenterStorage.class);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
presenters.add((Presenter) invocation.getArguments()[0]);
return null;
}
}).when(storage).add(any(Presenter.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return "" + presenters.indexOf(invocation.getArguments()[0]);
}
}).when(storage).getId(any(Presenter.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return presenters.get(Integer.parseInt((String) invocation.getArguments()[0]));
}
}).when(storage).getPresenter(anyString());
Whitebox.setInternalState(PresenterStorage.class, "INSTANCE", storage);
return storage;
}
代码示例来源:origin: apache/flink
final AbstractFetcher<String, ?> fetcher = mock(AbstractFetcher.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
代码示例来源:origin: julian-klode/dns66
@Before
public void setUp() {
mockStatic(Log.class);
mockContext = mock(Context.class);
file = mock(File.class);
singleWriterMultipleReaderFile = mock(SingleWriterMultipleReaderFile.class);
connection = mock(HttpURLConnection.class);
finishAnswer = new CountingAnswer(null);
failAnswer = new CountingAnswer(null);
realTask = new RuleDatabaseUpdateTask(mockContext, null, false);
mockTask = mock(RuleDatabaseUpdateTask.class);
url = mock(URL.class);
mockResolver = mock(ContentResolver.class);
try {
when(url.openConnection()).thenReturn(connection);
when(mockContext.getContentResolver()).thenReturn(mockResolver);
doAnswer(finishAnswer).when(singleWriterMultipleReaderFile, "finishWrite", any(FileOutputStream.class));
doAnswer(failAnswer).when(singleWriterMultipleReaderFile, "failWrite", any(FileOutputStream.class));
} catch (Exception e) {
e.printStackTrace();
}
}
代码示例来源:origin: julian-klode/dns66
persistedPermissions.add(garbagePermission);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
代码示例来源:origin: braintree/braintree_android
private void mockSuccessCallback() {
mockStatic(TokenizationClient.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(new CardNonce());
return null;
}
}).when(TokenizationClient.class);
TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class),
any(PaymentMethodNonceCallback.class));
}
代码示例来源:origin: braintree/braintree_android
private void mockFailureCallback() {
mockStatic(TokenizationClient.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, ""));
return null;
}
}).when(TokenizationClient.class);
TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class),
any(PaymentMethodNonceCallback.class));
}
}
代码示例来源:origin: braintree/braintree_android
private void mockFailureCallback() {
mockStatic(TokenizationClient.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, ""));
return null;
}
}).when(TokenizationClient.class);
TokenizationClient.tokenize(any(BraintreeFragment.class), any(UnionPayCardBuilder.class),
any(PaymentMethodNonceCallback.class));
}
}
代码示例来源:origin: braintree/braintree_android
private void mockConfigurationManager(final Configuration configuration) {
mockStatic(ConfigurationManager.class);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[1] != null) {
((ConfigurationListener) invocation.getArguments()[1]).onConfigurationFetched(configuration);
}
return null;
}
}).when(ConfigurationManager.class);
ConfigurationManager.getConfiguration(any(BraintreeFragment.class), any(ConfigurationListener.class),
any(BraintreeResponseListener.class));
}
代码示例来源:origin: braintree/braintree_android
private void mockConfigurationManager(final Exception exeption) {
mockStatic(ConfigurationManager.class);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[2] != null) {
((BraintreeResponseListener<Exception>) invocation.getArguments()[2]).onResponse(exeption);
}
return null;
}
}).when(ConfigurationManager.class);
ConfigurationManager.getConfiguration(any(BraintreeFragment.class), any(ConfigurationListener.class),
any(BraintreeResponseListener.class));
}
}
代码示例来源:origin: braintree/braintree_android
@Test
public void fetchIssuingBanks_failure_sendsAnalyticsEvent() {
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
callback.failure(new IOException());
return null;
}
}).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
Ideal.fetchIssuingBanks(mBraintreeFragment, null);
verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.load.failed"));
}
代码示例来源:origin: braintree/braintree_android
@Test
public void startPayment_callsExceptionListenerOnHttpError() throws InvalidArgumentException, JSONException {
final Exception expectedException = new Exception();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2];
callback.failure(expectedException);
return null;
}
}).when(mMockHttpClient).post(eq("/v1/paypal_hermes/create_payment_resource"), any(String.class), any(HttpResponseCallback.class));
LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(), new BraintreeResponseListener<LocalPaymentRequest>() {
@Override
public void onResponse(LocalPaymentRequest localPaymentRequest) {
LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest);
}
});
verify(mBraintreeFragment).postCallback(eq(expectedException));
}
代码示例来源:origin: braintree/braintree_android
@Test
public void fetchIssuingBanks_postsCallbackToFragment() throws InvalidArgumentException {
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
callback.failure(new IOException());
return null;
}
}).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {
@Override
public void onResponse(List<IdealBank> idealBanks) {
fail("Success listener called");
}
});
verify(mBraintreeFragment).postCallback(any(IOException.class));
}
代码示例来源:origin: braintree/braintree_android
@Test
public void fetchIssuingBanks_postsErrorToListenerOnJsonParsingError() throws InvalidArgumentException {
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
callback.success("gibberish");
return null;
}
}).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {
@Override
public void onResponse(List<IdealBank> idealBanks) {
fail("Success listener called");
}
});
verify(mBraintreeFragment).postCallback(any(JSONException.class));
}
代码示例来源:origin: braintree/braintree_android
@Test
public void startPayment_callsExceptionListenerOnJsonParsingError()
throws InvalidArgumentException, JSONException, InterruptedException {
putResultIdInPrefs("ideal_payment_id");
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
callback.success("gibberish");
latch.countDown();
return null;
}
}).when(mMockApiClient).get(eq("/ideal-payments/ideal_payment_id/status"), any(HttpResponseCallback.class));
Ideal.onActivityResult(mBraintreeFragment, Activity.RESULT_OK);
latch.await();
verify(mBraintreeFragment).postCallback(any(JSONException.class));
}
代码示例来源:origin: braintree/braintree_android
@Test
public void authorizeAccount_callsCancelListenerWhenCanceled() {
final BraintreeFragment fragment = mMockFragmentBuilder.build();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Intent intent = new Intent()
.setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/cancel"));
PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent);
return null;
}
}).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class));
PayPal.authorizeAccount(fragment);
verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL);
}
代码示例来源:origin: braintree/braintree_android
@Test
public void fetchIssuingBanks_success_sendsAnalyticsEvent() {
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
callback.success(stringFromFixture("payment_methods/ideal_issuing_banks.json"));
return null;
}
}).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {
@Override
public void onResponse(List<IdealBank> idealBanks) {
verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.load.succeeded"));
}
});
}
代码示例来源:origin: bjoernQ/unmock-plugin
@Test
public void testDelegateClass() throws Exception {
mockStatic(ABridge.class);
final boolean[] called = new boolean[]{false};
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
called[0] = true;
return null;
}
}).when(ABridge.class, "callObject", eq("android.os.AsyncTask.execute(java.lang.Object[])"), anyObject(), any(Object[].class));
AsyncTaskSubclass sut = new AsyncTaskSubclass();
sut.execute();
assertTrue(called[0]);
}
}
代码示例来源:origin: braintree/braintree_android
@Test
public void requestBillingAgreement_cancelUrlTriggersCancelListener() {
final BraintreeFragment fragment = mMockFragmentBuilder
.successResponse(stringFromFixture("paypal_hermes_response.json"))
.build();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Intent intent = new Intent()
.setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/cancel"));
PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent);
return null;
}
}).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class));
PayPal.requestBillingAgreement(fragment, new PayPalRequest());
verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL);
}
代码示例来源:origin: braintree/braintree_android
@Test
public void checkout_cancelUrlTriggersCancelListener() {
final BraintreeFragment fragment = mMockFragmentBuilder
.successResponse(stringFromFixture("paypal_hermes_response.json"))
.build();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Intent intent = new Intent()
.setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/cancel"));
PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent);
return null;
}
}).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class));
PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"));
verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL);
}
内容来源于网络,如有侵权,请联系作者删除!