Skip to content

Commit 43d6ac9

Browse files
Improve test infra
1 parent 8cc9a64 commit 43d6ac9

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

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

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Set;
27+
import java.util.function.Consumer;
2728
import lombok.Cleanup;
2829
import org.junit.jupiter.api.AfterEach;
2930
import org.junit.jupiter.api.BeforeAll;
@@ -192,6 +193,27 @@ public static <T extends StripeObjectInterface> void verifyRequest(
192193
RequestOptions options)
193194
throws StripeException {
194195

196+
verifyRequest(
197+
(req) -> {
198+
assertEquals(baseAddress, req.getBaseAddress());
199+
assertEquals(method, req.getMethod());
200+
assertEquals(path, req.getPath());
201+
if (params != null) {
202+
final String msg =
203+
String.format(
204+
"Params did not match - expected: %s, received: %s", params, req.getParams());
205+
assertTrue(msg, compareParamObjects(params, req.getParams()));
206+
}
207+
if (options != null) {
208+
assertEquals(options, req.getOptions());
209+
}
210+
});
211+
}
212+
213+
@SuppressWarnings("AssertionFailureIgnored")
214+
public static <T extends StripeObjectInterface> void verifyRequest(
215+
Consumer<ApiRequest> assertOnApiRequest) throws StripeException {
216+
195217
ArgumentCaptor<ApiRequest> requestCaptor = ArgumentCaptor.forClass(ApiRequest.class);
196218
List<AssertionError> exceptions = new ArrayList<AssertionError>();
197219
try {
@@ -203,19 +225,35 @@ public static <T extends StripeObjectInterface> void verifyRequest(
203225

204226
for (ApiRequest req : requestCaptor.getAllValues()) {
205227
try {
206-
assertEquals(baseAddress, req.getBaseAddress());
207-
assertEquals(method, req.getMethod());
208-
assertEquals(path, req.getPath());
209-
if (params != null) {
210-
final String msg =
211-
String.format(
212-
"Params did not match - expected: %s, received: %s", params, req.getParams());
213-
assertTrue(msg, compareParamObjects(params, req.getParams()));
214-
}
215-
if (options != null) {
216-
assertEquals(options, req.getOptions());
217-
}
218-
// If we get here, we have found a request that triggered no assertion failures.
228+
assertOnApiRequest.accept(req);
229+
return;
230+
} catch (AssertionError e) {
231+
exceptions.add(e);
232+
}
233+
}
234+
235+
// If we get here, each request failed an assertion.
236+
if (exceptions.size() != 0) {
237+
// Combine all exceptions into a single message
238+
String msg = "";
239+
for (AssertionError e : exceptions) {
240+
msg += e.getMessage() + "\n\n";
241+
}
242+
throw new AssertionError(msg);
243+
}
244+
}
245+
246+
@SuppressWarnings("AssertionFailureIgnored")
247+
public static <T extends StripeObjectInterface> void verifyStripeRequest(
248+
Consumer<StripeRequest> assertOnStripeRequest) throws StripeException {
249+
250+
ArgumentCaptor<StripeRequest> requestCaptor = ArgumentCaptor.forClass(StripeRequest.class);
251+
List<AssertionError> exceptions = new ArrayList<AssertionError>();
252+
Mockito.verify(httpClientSpy, Mockito.atLeastOnce()).request(requestCaptor.capture());
253+
254+
for (StripeRequest req : requestCaptor.getAllValues()) {
255+
try {
256+
assertOnStripeRequest.accept(req);
219257
return;
220258
} catch (AssertionError e) {
221259
exceptions.add(e);
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.stripe.functional;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import com.google.gson.Gson;
46
import com.google.gson.JsonObject;
57
import com.stripe.BaseStripeTest;
@@ -8,7 +10,6 @@
810
import org.junit.jupiter.api.AfterEach;
911
import org.junit.jupiter.api.BeforeEach;
1012
import org.junit.jupiter.api.Test;
11-
import org.mockito.Mockito;
1213

1314
public class StripeClientTest extends BaseStripeTest {
1415

@@ -28,22 +29,20 @@ public void tearDown() {
2829
@Test
2930
public void testReportsUsageTelemetry() throws StripeException {
3031
mockClient.customers().create();
31-
Mockito.reset(httpClientSpy);
3232
mockClient.customers().update("cus_xyz");
33-
Mockito.verify(httpClientSpy)
34-
.request(
35-
Mockito.argThat(
36-
stripeRequest -> {
37-
String usage =
38-
new Gson()
39-
.fromJson(
40-
stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").get(),
41-
JsonObject.class)
42-
.get("last_request_metrics")
43-
.getAsJsonObject()
44-
.get("usage")
45-
.getAsString();
46-
return usage.equals("stripe_client");
47-
}));
33+
verifyStripeRequest(
34+
(stripeRequest) -> {
35+
assert(stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").isPresent());
36+
String usage =
37+
new Gson()
38+
.fromJson(
39+
stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").get(),
40+
JsonObject.class)
41+
.get("last_request_metrics")
42+
.getAsJsonObject()
43+
.get("usage")
44+
.getAsString();
45+
assertEquals("stripe_client", usage);
46+
});
4847
}
4948
}

0 commit comments

Comments
 (0)