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": [
{
"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
{
"name":"hoge",
"age":"10",
"id":"001"
}
{
"Received headers.User-Agent": PostmanRuntime/7.36.3,
"receivedBody": {
"name": "hoge",
"age": "10",
"id": "001"
}
}
get
https://localhost:8443/all_response/get?param1=hoge¶m2=tanaka
{
"Received headers.User-Agent": PostmanRuntime/7.36.3,
"param1": hoge,
"param2": tanaka
}
put
https://localhost:8443/all_response/put
{
"name":"hoge",
"age":"10",
"id":"001"
}
{
{
"Received headers.User-Agent": PostmanRuntime/7.36.3,
"receivedBody": {
"name": "hoge",
"age": "10",
"id": "001"
}
}