개발

WireMock Introduce~!

쩌비군 2020. 5. 26. 19:23
728x90

WireMock 은 HTTP 기반 API의 시뮬레이터로 API 가상화, Mocking 서비스를 제공한다. API 가 개발 중인 경우 WireMock 을 이용해 엣지 케이스 및 실패 테스트를 수행 할 수 있다(http://wiremock.org)

 

주요 기능

API 가상화, Mocking 서비스 기능

Stand Alone Wire Mock 서버 기능

Record & Play 기능으로 Mock 서버 구성 기능

 

  https://www.slideshare.net/koenighotze/resilience-testing-with-wiremock-and-spock  

 

 

WireMock 환경 구성

WireMock 환경 구성은 아주 간단하다. 아래와 같이 Maven Dependency만 추가 해주면 된다.

<!-- Wire MOCK -->
 <dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock</artifactId>
    <version>2.5.1</version>
</dependency>

 

 


WireMock & Rest Assured 코드 예제

WireMock 을 이용하면 Rest API 를 Unit Test Code 상에서 Mock 을 구현하여 빠르게 실행 할 수 있다

* 예제 1번 : [Get 예제]

Public class WeatherApplicationTest {
   @Rule
   public WireMockRule wireMockRule = new WireMockRule(8090);
   @Before
   public void steup() {
      RestAssured.baseURI = "http://localhost";
      RestAssured.port    = 8090;
   }
 
   @Test
   public  void testGetURLPath() throws InterruptedException {
      wireMockRule.stubFor(
                        get(urlEqualTo("/forecast/seoul"))
                        .willReturn(
                           aResponse()
                              .withHeader("Content-Type", "application/json")
                              .withStatus(200)
                              .withBody("   {"
                                           +"\"currently\": "
                                           + "{"
                                           + " \"windSpeed\":12.34 "
                                           + "}"
                                           + "}"))
                            );
      Response res = RestAssured
                                .given()
                                       .pathParam("city", "seoul") 
                                .when()
                                       .get("/forecast/{city}")
                                .then()
                                      .statusCode(200)
                                      .body("currently.windSpeed", is(12.34f))
                                      .extract().response();
}

 

 

 

* 예제 2번 : [Cookie 예제]

@Test
public  void testGetCookie() {
   wireMockRule
        .stubFor(get(urlEqualTo("/forecast/cookies"))
                     .withCookie(“tuid", containing(“tuidValid"))
                     .willReturn(aResponse()
                                 .withHeader("Content-Type", "application/text/html")
                                 .withStatus(200))
                   );
  
   RequestSpecBuilder requestBuilder = new RequestSpecBuilder();
   requestBuilder.addCookie(“tuid", “tuidValid");
   requestBuilder.setContentType("application/json");
   RestAssured.requestSpecification = requestBuilder.build();
   RestAssured
              .given()
              .when()
                    .get("/forecast/cookies")
              .then()
                    .statusCode(200); 
}