2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WireMockを使用してローカル環境でAPIテストを行う

Posted at

WireMockとは

WireMockは、APIテストのためのオープンソースツールです。開発者は、スタブと呼ばれる仮想のAPIサーバーを作成することで、APIの動作をシミュレートすることができます。これにより、実際のAPIが利用できない場合でも、APIクライアントのテストを実施することができます。

WireMockを使う利点

  • 実際のAPIに依存せずにテストできる: WireMockを使えば、実際のAPIが利用できない場合でも、APIクライアントのテストを実施することができます。
  • テストを迅速化できる: WireMockを使えば、実際のAPIを呼び出すよりもはるかに高速にテストを実行することができます。
  • テストを信頼性高める: WireMockを使えば、さまざまな状況をシミュレートすることで、テストの信頼性を高めることができます。

WireMockの始め方

WireMockは、公式サイトからダウンロードすることができます。
https://wiremock.org/docs/
今回はスタンドアローンで起動させるためにjarをDLする
https://repo1.maven.org/maven2/org/wiremock/wiremock-standalone/3.4.1/wiremock-standalone-3.4.1.jar

WireMockを起動する

任意のディレクトリにDLしたjarを移動。(WireMockを初回起動した時に【__files】と【mappings】ディレクトリが作成されるので注意)

起動コマンド実行
java -jar wiremock-standalone-3.4.1.jar --global-response-templating --https-port 8443

起動を確認


██     ██ ██ ██████  ███████ ███    ███  ██████   ██████ ██   ██ 
██     ██ ██ ██   ██ ██      ████  ████ ██    ██ ██      ██  ██  
██  █  ██ ██ ██████  █████   ██ ████ ██ ██    ██ ██      █████   
██ ███ ██ ██ ██   ██ ██      ██  ██  ██ ██    ██ ██      ██  ██  
███ ███  ██ ██   ██ ███████ ██      ██  ██████   ██████ ██   ██ 

----------------------------------------------------------------
|               Cloud: https://wiremock.io/cloud               |
|                                                              |
|               Slack: https://slack.wiremock.org              |
----------------------------------------------------------------

version:                      3.4.1
port:                         8080
https-port:                   8443

mapping作成

mappingディレクトリ配下にリクエストとレスポンス情報をJSONで設定します。
今回はhttpメソッドの「post」「get」「put」に対応。
作成したら【mapping】ディレクトリに格納する。

mappings.json
{
    "mappings": [
        {
            "request": {
                "method": "POST",
                "url": "/all_response/post"
            },
            "response": {
                "status": 200,
                "body": "{ \"Received headers.User-Agent\": {{request.headers.User-Agent}}, \n \"receivedBody\": {{request.body}} }",
                "headers": {
                    "Content-Type": "application/json"
                },
                "transformers": [
                    "response-template"
                ]
            }
        },
        {
            "request": {
                "method": "GET",
                "urlPathPattern": "/all_response/get",
                "queryParameters": {
                    "param1": {
                        "matches": ".*"
                    },
                    "param2": {
                        "matches": ".*"
                    }
                }
            },
            "response": {
                "status": 200,
                "body": "{ \"Received headers.User-Agent\": {{request.headers.User-Agent}}, \n \"param1\": {{request.query.param1}}, \n\"param2\": {{request.query.param2}} }",
                "headers": {
                    "Content-Type": "application/json"
                },
                "transformers": [
                    "response-template"
                ]
            }
        },
        {
            "request": {
                "method": "PUT",
                "url": "/all_response/put"
            },
            "response": {
                "status": 200,
                "body": "{ { \"Received headers.User-Agent\": {{request.headers.User-Agent}}, \n \"receivedBody\": {{request.body}} }",
                "headers": {
                    "Content-Type": "application/json"
                },
                "transformers": [
                    "response-template"
                ]
            }
        }
    ]
}

テスト

postmanを使用してhttpリクエストを行う。
mappings.json内のrequestのmethodとurlでリクエストの設定を行っている

post
https://localhost:8443/all_response/post

request body
{
    "name":"hoge",
    "age":"10",
    "id":"001"
}
response body
{
    "Received headers.User-Agent": PostmanRuntime/7.36.3,
    "receivedBody": {
        "name": "hoge",
        "age": "10",
        "id": "001"
    }
}

get
https://localhost:8443/all_response/get?param1=hoge&param2=tanaka

response body
{
    "Received headers.User-Agent": PostmanRuntime/7.36.3,
    "param1": hoge,
    "param2": tanaka
}

put
https://localhost:8443/all_response/put

request body
{
    "name":"hoge",
    "age":"10",
    "id":"001"
}
response body
{
    {
        "Received headers.User-Agent": PostmanRuntime/7.36.3,
        "receivedBody": {
            "name": "hoge",
            "age": "10",
            "id": "001"
        }
    }
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?