Skip to content

Commit 4bbffce

Browse files
Add generated v2 test support (#1953)
* Add a method to stub requests that return an error status * Fix semicolons * rebase and update
1 parent e96e0d6 commit 4bbffce

File tree

2 files changed

+434
-121
lines changed

2 files changed

+434
-121
lines changed

src/test/java/com/stripe/BaseStripeTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.Collections;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Set;
@@ -32,6 +33,7 @@
3233
import org.mockito.ArgumentMatcher;
3334
import org.mockito.Mockito;
3435
import org.mockito.exceptions.base.MockitoAssertionError;
36+
import org.mockito.stubbing.Answer;
3537

3638
public class BaseStripeTest {
3739
private static final String MOCK_MINIMUM_VERSION = "0.109.0";
@@ -348,6 +350,35 @@ public static <T extends StripeObjectInterface> void stubRequest(
348350
Mockito.<Type>any());
349351
}
350352

353+
/**
354+
* Stubs an API request. This should rarely be necessary, but some endpoints are not supported by
355+
* stripe-mock yet.
356+
*
357+
* @param method HTTP method (GET, POST or DELETE)
358+
* @param path request path (e.g. "/v1/charges"). Can also be an abolute URL.
359+
* @param params map containing the parameters. If null, the parameters are not checked.
360+
* @param options request options. If null, the options are not checked.
361+
* @param typeToken Class of the API resource that will be returned for the stubbed request.
362+
* @param response JSON payload of the API resource that will be returned for the stubbed request.
363+
*/
364+
public static <T extends StripeObjectInterface> void stubRequestReturnError(
365+
BaseAddress baseAddress,
366+
ApiResource.RequestMethod method,
367+
String path,
368+
Map<String, Object> params,
369+
RequestOptions options,
370+
String response,
371+
Integer code)
372+
throws StripeException {
373+
374+
Mockito.doAnswer(
375+
(Answer<StripeResponse>)
376+
invocation ->
377+
new StripeResponse(400, HttpHeaders.of(Collections.emptyMap()), response))
378+
.when(httpClientSpy)
379+
.request(Mockito.any());
380+
}
381+
351382
/** Stubs an OAuth API request. stripe-mock does not supported OAuth endpoints at this time. */
352383
public static <T extends StripeObjectInterface> void stubOAuthRequest(
353384
Class<T> clazz, String response) throws StripeException {

0 commit comments

Comments
 (0)