1
+ package com .baeldung .cloud .openfeign .completablefuturefeignclient ;
2
+
3
+ import com .baeldung .cloud .openfeign .ExampleApplication ;
4
+ import com .github .tomakehurst .wiremock .WireMockServer ;
5
+
6
+ import org .junit .jupiter .api .AfterEach ;
7
+ import org .junit .jupiter .api .BeforeEach ;
8
+ import org .junit .jupiter .api .Disabled ;
9
+ import org .junit .jupiter .api .Test ;
10
+ import org .junit .jupiter .api .extension .ExtendWith ;
11
+ import org .springframework .beans .factory .annotation .Autowired ;
12
+ import org .springframework .boot .test .context .SpringBootTest ;
13
+ import org .springframework .http .HttpStatus ;
14
+ import org .springframework .test .context .junit .jupiter .SpringExtension ;
15
+
16
+ import java .util .concurrent .ExecutionException ;
17
+
18
+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
19
+ import static com .github .tomakehurst .wiremock .client .WireMock .configureFor ;
20
+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
21
+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
22
+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
23
+ import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
24
+ import static org .junit .Assert .*;
25
+
26
+ @ ExtendWith (SpringExtension .class )
27
+ @ SpringBootTest (classes = ExampleApplication .class )
28
+ class PurchaseServiceIntegrationTest {
29
+
30
+ @ Autowired
31
+ private PurchaseService purchaseService ;
32
+
33
+ @ Autowired
34
+ private PaymentMethodClient paymentMethodClient ;
35
+
36
+ @ Autowired
37
+ private ReportClient reportClient ;
38
+
39
+ private WireMockServer wireMockServer ;
40
+
41
+ @ BeforeEach
42
+ public void startWireMockServer () {
43
+ wireMockServer = new WireMockServer (8083 );
44
+ configureFor ("localhost" , 8083 );
45
+ wireMockServer .start ();
46
+
47
+ stubFor (post (urlEqualTo ("/reports" )).willReturn (aResponse ().withStatus (HttpStatus .OK .value ())));
48
+ }
49
+
50
+ @ AfterEach
51
+ public void stopWireMockServer () {
52
+ wireMockServer .stop ();
53
+ }
54
+
55
+ @ Test
56
+ void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult () throws ExecutionException , InterruptedException {
57
+ stubFor (get (urlEqualTo ("/payment_methods?site_id=BR" )).willReturn (aResponse ().withStatus (HttpStatus .OK .value ())
58
+ .withBody ("credit_card" )));
59
+
60
+ String result = purchaseService .executePurchase ("BR" );
61
+
62
+ assertNotNull (result );
63
+ assertEquals ("Purchase executed with payment method credit_card" , result );
64
+ }
65
+
66
+ @ Test
67
+ void givenRestCalls_whenPurchaseReturns404_thenReturnDefault () throws ExecutionException , InterruptedException {
68
+ stubFor (get (urlEqualTo ("/payment_methods?site_id=BR" )).willReturn (aResponse ().withStatus (HttpStatus .NOT_FOUND .value ())));
69
+
70
+ String result = purchaseService .executePurchase ("BR" );
71
+
72
+ assertNotNull (result );
73
+ assertEquals ("Purchase executed with payment method cash" , result );
74
+ }
75
+
76
+ @ Test
77
+ @ Disabled
78
+ void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenThrowNewException () {
79
+ stubFor (get (urlEqualTo ("/payment_methods?site_id=BR" )).willReturn (aResponse ().withFixedDelay (550 )));
80
+
81
+ Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase ("BR" ));
82
+
83
+ assertEquals ("java.lang.RuntimeException: Thread timeout!" , error .getMessage ());
84
+ }
85
+
86
+ @ Test
87
+ void givenRestCalls_whenPurchaseRequestWebTimeout_thenThrowNewException () {
88
+ stubFor (get (urlEqualTo ("/payment_methods?site_id=BR" )).willReturn (aResponse ().withFixedDelay (250 )));
89
+
90
+ Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase ("BR" ));
91
+
92
+ assertEquals ("java.lang.RuntimeException: REST call network timeout!" , error .getMessage ());
93
+ }
94
+ }
0 commit comments