1
1
package com .codebykavindu .bookstore .orders ;
2
2
3
+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
4
+ import static com .github .tomakehurst .wiremock .client .WireMock .configureFor ;
5
+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
6
+ import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
3
7
import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment .RANDOM_PORT ;
4
8
9
+ import com .github .tomakehurst .wiremock .client .WireMock ;
5
10
import io .restassured .RestAssured ;
11
+ import java .math .BigDecimal ;
12
+ import org .junit .jupiter .api .BeforeAll ;
6
13
import org .junit .jupiter .api .BeforeEach ;
7
14
import org .springframework .boot .test .context .SpringBootTest ;
8
15
import org .springframework .boot .test .web .server .LocalServerPort ;
9
16
import org .springframework .context .annotation .Import ;
17
+ import org .springframework .http .MediaType ;
18
+ import org .springframework .test .context .DynamicPropertyRegistry ;
19
+ import org .springframework .test .context .DynamicPropertySource ;
20
+ import org .wiremock .integrations .testcontainers .WireMockContainer ;
10
21
11
22
/**
12
23
* @author Kavindu Perera
@@ -17,8 +28,37 @@ public abstract class AbstractIntegrationTest {
17
28
@ LocalServerPort
18
29
int port ;
19
30
31
+ static WireMockContainer wiremockServer = new WireMockContainer ("wiremock/wiremock:3.5.2-alpine" );
32
+
33
+ @ BeforeAll
34
+ static void beforeAll () {
35
+ wiremockServer .start ();
36
+ configureFor (wiremockServer .getHost (), wiremockServer .getPort ());
37
+ }
38
+
39
+ @ DynamicPropertySource
40
+ static void configureProperties (DynamicPropertyRegistry registry ) {
41
+ registry .add ("orders.catalog-service-url" , wiremockServer ::getBaseUrl );
42
+ }
43
+
20
44
@ BeforeEach
21
45
void setUp () {
22
46
RestAssured .port = port ;
23
47
}
48
+
49
+ protected static void mockGetProductByCode (String code , String name , BigDecimal price ) {
50
+ stubFor (WireMock .get (urlMatching ("/api/v1/products/" + code ))
51
+ .willReturn (aResponse ()
52
+ .withHeader ("Content-Type" , MediaType .APPLICATION_JSON_VALUE )
53
+ .withStatus (200 )
54
+ .withBody (
55
+ """
56
+ {
57
+ "code": "%s",
58
+ "name": "%s",
59
+ "price": %f
60
+ }
61
+ """
62
+ .formatted (code , name , price .doubleValue ()))));
63
+ }
24
64
}
0 commit comments