WireMock ์‚ฌ์šฉ

· โ˜• 1 min read · ๐Ÿ‘€... views

์ˆœ์„œ๊ฐ€ ์–ด๊ธ‹๋‚œ ๊ฐ์ด ์žˆ๊ธด ํ•˜์ง€๋งŒ, ์ง€๋‚œ ๋ฒˆ API ํ†ต์‹ ์„ ์œ„ํ•ด Feign ์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ,
ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ ์‹ค์ œ๋กœ API ์ ‘์†์„ ํ•  ์ˆ˜๋Š” ์—†์œผ๋‹ˆ Mock Web ์„œ๋ฒ„๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ์ˆ ์ธ WireMock ๋ฅผ ์‚ฌ์šฉํ•œ ๋‚ด์šฉ์„ ์˜ฌ๋ฆฌ๊ณ ์ž ํ•œ๋‹ค.

WireMock ์ด๋ž€?

  • HTTP-based API๋ฅผ ์œ„ํ•œ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ
  • Mock ์œผ๋กœ ๋งŒ๋“œ๋Š” ์›น์„œ๋ฒ„
  • http://wiremock.org/

1. ์˜์กด์„ฑ ์ถ”๊ฐ€

  • Maven ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ pom.xml ์— ์˜์กด์„ฑ ์ถ”๊ฐ€
1
2
3
4
5
6
7
<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8</artifactId>
    <version>2.24.1</version>
    <scope>test</scope>
</dependency>

2. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Test {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(8089); // ์ž‘์„ฑํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ default 8080    
    
    private BoardServiceService service;
    ...
    
    @Test
    public void ๋ชฉ๋ก_test() {
        
        StubMapping accept = stubFor(
                get(urlPathMatching("/board/([a-zA-Z0-9/-]*)")) // board/71 ๊ฐ™์€ ๊ฑธ ์ฐพ๊ธฐ ์œ„ํ•ด ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ
                        .willReturn(aResponse()
                                .withStatus(200)
                                .withHeader("content-type", "application/json")
                                .withBody("Hello world!")
                        )
        );
        ApiResponse<Data> ret = service.getBoard(71); // ์„œ๋น„์Šค์—์„œ API ํ˜ธ์ถœ (/board/71)
        assertEquals("200", ret.getCode());
        assertEquals("OK", ret.getMessage());
        ...
    }
}
  • Junit ์„ ์ง€์›ํ•˜๊ธฐ์— ์ข€ ํŽธํ•˜๊ฒŒ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Œ. (5์—์„œ๋„ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๊ธดํ•œ๋ฐ…)
  • sample ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์˜€์ง€๋งŒ, ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์ด ๋งŽ๋‹ค. (https, proxy, ์ƒํƒœ์ €์žฅ ๋“ฑ)
  • ์ข€ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ WireMock ํŽ˜์ด์ง€์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ
Share on

snack
WRITTEN BY
snack
Web Programmer


What's on this Page