[Server]Mock Server(목서버)는 어때요?

오늘 회의에서 신규서비스 테스트를 위해 Mock서버, jUnit단위테스트 등의 이야기가 나왔다.
외부 API를 테스트를 하려면 어떻게 해야할까?

jUnit에 관해서는 이전에 작성한 Junit 포스팅을 참고하면 된다.
그럼 목서버는 무엇일까?

먼저 MOCK(목)이 뭘까?

실제 사용되어야하는 객체의 대체객체로 실제 객체를 만들어서 하기엔 시간이 부족하고 비용이 높은 경우, 구현이 까다로울 경우에 가짜 객체를 만들어서 사용하는데 그중 하나가 Mock객체이다.
Mock객체: 행위를 검증하기 위해 사용되는 객체
직접 만들거나 스프링프레임워크를 통해서 간단하게 만들수 있음
출처: Junit - Mock 객체




Mock 서버란?

For any system you integrate with via HTTP or HTTPS MockServer can be used as:
a mock configured to return specific responses for different requests
a proxy recording and optionally modifying requests and responses
both a proxy for some requests and a mock for other requests at the same time
When MockServer receives a request it matches the request against active expectations that have been configured.
Then, if no matches are found, it proxies the request if appropriate; otherwise a 404 is returned.
For each request received the following steps happen:
find matching expectation and perform action
if no matching expectation proxy request
if not a proxy request return 404
An expectation defines the action that is taken, for example, a response could be returned.
출처: https://www.mock-server.com/

간단하게 말하자면, 실제 서버처럼 HTTP나 HTTPS 요청과 응답을 주고 받을 수 있는 가짜 서버이다.

  • 다양한 요청에 특정한 반응을 반환하도록 구성할 수 있음
  • 프록시를 기록하고 선택적으로 요청과 응답을 수정할 수 있음
  • 목서버가 예상되는 요청을 받았을땐 이미 작성된 특정 반응을 반환하고 예상되지 않는 요청인 경우 404를 리턴한다.




왜 목서버를 사용할까?

크게 3가지 이유가 있다.

  • testing via HTTP or HTTPS, such as a REST or RPC service.
    • 만약 스펙만 존재하고 실제 동작하지 않는 API를 염두하고 해당 API의 데이터를 받는 부분을 개발해야 한다면 어떻게 해야할까? 이럴 때 필요한 것이 가상의 서버, 바로 Mock Server이다.
  • de-coupling development
  • isolate single service




목서버를 만드는 방법

구글링했을때 가장 많이 나오는 방법은 PostMan을 활용하는 것이다. 하지만 무료요금제인 경우 한달에 1000건 요청제한이 있다.
Postman 으로 Mock Server 구축하기글을 참고!




출처

Comments