44import com .github .tomakehurst .wiremock .WireMockServer ;
55import org .junit .jupiter .api .AfterEach ;
66import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .Disabled ;
78import org .junit .jupiter .api .Test ;
89import org .junit .jupiter .api .extension .ExtendWith ;
910import org .springframework .beans .factory .annotation .Autowired ;
1011import org .springframework .boot .test .context .SpringBootTest ;
11- import org .springframework .cloud .openfeign .FeignClientProperties ;
1212import org .springframework .http .HttpStatus ;
13- import org .springframework .test .context .TestPropertySource ;
1413import org .springframework .test .context .junit .jupiter .SpringExtension ;
1514
1615import java .util .concurrent .ExecutionException ;
17- import java .util .concurrent .TimeoutException ;
1816
19- import static com .github .tomakehurst .wiremock .client .WireMock .*;
17+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
18+ import static com .github .tomakehurst .wiremock .client .WireMock .configureFor ;
19+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
20+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
21+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
22+ import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
2023import static org .junit .Assert .*;
21- import static org .mockito .ArgumentMatchers .any ;
2224
2325@ ExtendWith (SpringExtension .class )
2426@ SpringBootTest (classes = ExampleApplication .class )
25- @ TestPropertySource (locations = "classpath:application-integration_test.properties" )
2627class PurchaseServiceIntegrationTest {
2728
2829 @ Autowired
@@ -36,8 +37,6 @@ class PurchaseServiceIntegrationTest {
3637
3738 private WireMockServer wireMockServer ;
3839
39- private Purchase purchase ;
40-
4140 @ BeforeEach
4241 public void startWireMockServer () {
4342 wireMockServer = new WireMockServer (8083 );
@@ -46,8 +45,6 @@ public void startWireMockServer() {
4645
4746 stubFor (post (urlEqualTo ("/reports" ))
4847 .willReturn (aResponse ().withStatus (HttpStatus .OK .value ())));
49-
50- purchase = new Purchase ("BR" );
5148 }
5249
5350 @ AfterEach
@@ -58,10 +55,10 @@ public void stopWireMockServer() {
5855 @ Test
5956 void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult ()
6057 throws ExecutionException , InterruptedException {
61- stubFor (post (urlEqualTo ("/purchase ?site_id=BR" ))
58+ stubFor (get (urlEqualTo ("/payment_methods ?site_id=BR" ))
6259 .willReturn (aResponse ().withStatus (HttpStatus .OK .value ()).withBody ("credit_card" )));
6360
64- String result = purchaseService .executePurchase (purchase );
61+ String result = purchaseService .executePurchase ("BR" );
6562
6663 assertNotNull (result );
6764 assertEquals ("Purchase executed with payment method credit_card" , result );
@@ -70,32 +67,33 @@ void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult()
7067 @ Test
7168 void givenRestCalls_whenPurchaseReturns404_thenReturnDefault ()
7269 throws ExecutionException , InterruptedException {
73- stubFor (post (urlEqualTo ("/purchase ?site_id=BR" ))
70+ stubFor (get (urlEqualTo ("/payment_methods ?site_id=BR" ))
7471 .willReturn (aResponse ().withStatus (HttpStatus .NOT_FOUND .value ())));
7572
76- String result = purchaseService .executePurchase (purchase );
73+ String result = purchaseService .executePurchase ("BR" );
7774
7875 assertNotNull (result );
79- assertEquals ("Purchase executed with payment method account_money " , result );
76+ assertEquals ("Purchase executed with payment method cash " , result );
8077 }
8178
8279 @ Test
83- void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenReturnDefault () {
84- stubFor (post (urlEqualTo ("/purchase?site_id=BR" ))
85- .willReturn (aResponse ().withFixedDelay (450 )));
80+ @ Disabled
81+ void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenThrowNewException () {
82+ stubFor (get (urlEqualTo ("/payment_methods?site_id=BR" ))
83+ .willReturn (aResponse ().withFixedDelay (550 )));
8684
87- Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase (purchase ));
85+ Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase ("BR" ));
8886
8987 assertEquals ("java.lang.RuntimeException: Thread timeout!" , error .getMessage ());
9088 }
9189
92- // @Test
93- // void givenRestCalls_whenPurchaseRequestWebTimeout_thenReturnDefault () {
94- // stubFor(post (urlEqualTo("/purchase ?site_id=BR"))
95- // .willReturn(aResponse().withFixedDelay(250)));
96- //
97- // Throwable error = assertThrows(ExecutionException.class, () -> purchaseService.executePurchase(purchase ));
98- //
99- // assertEquals("REST call network timeout!", error.getMessage());
100- // }
90+ @ Test
91+ void givenRestCalls_whenPurchaseRequestWebTimeout_thenThrowNewException () {
92+ stubFor (get (urlEqualTo ("/payment_methods ?site_id=BR" ))
93+ .willReturn (aResponse ().withFixedDelay (250 )));
94+
95+ Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase ("BR" ));
96+
97+ assertEquals ("java.lang.RuntimeException: REST call network timeout!" , error .getMessage ());
98+ }
10199}
0 commit comments