走進Java接口測試之簡單快速的Mock Server Moco

引言

在上文中,我們介紹 Mock 的基本概念,本文我們將詳細介紹其中一個快速簡單Mock Server Moco

簡介

簡單來說 Moco 就是類似一個 Mock 的工具框架,一個簡單搭建模擬服務器的程序庫 / 工具,下載就是一個JAR包。
在 Moco 的 github 上面有這段話。

Integration, especially based on HTTP protocol, e.g. web service, REST etc, is wildly used in most of our development.
In the old days, we just deployed another WAR to an application server, e.g. Jetty or Tomcat etc. As we all know, it’s so boring to develop a WAR and deploy it to any application server, even if we use an embeded server. And the WAR needs to be reassembled even if we just want to change a little bit.

翻譯過來:

集成,特別是基於 HTTP 協議的集成,例如 web 服務、REST 等,在我們的大多數開發中都被廣泛使用。
在過去,我們只是將另一場 WAR 包部署到應用服務器上,例如 Jetty 或Tomcat 等。衆所周知,開發一個 WAR 包並將其部署到任何應用服務器上是非常枯燥的,即使我們使用的是嵌入式服務器。war包也需要被重新打包即使我們只是想稍微改變一下。
簡單來說,Moco 就是解決了開發前端時沒有後端支持,開發接口時依賴沒有到位的尷尬場景。當然 Moco 的靈活性,讓其有越來越多的應用場景,比如我們在開發接口測試的時候。

特點:

  • 只需要簡單的配置 request、response 等即可滿足要求,支持 http、https、socket 。可以說是非常的靈活性。
  • 支持在 request 中設置 Headers , Cookies , StatusCode 等。
  • 對 GET、POST、PUT、DELETE 等請求方式均支持,很適合 web 開發。
  • 無需環境配置,有 Java 環境即可。
  • 修改配置後,立刻生效。只需要維護接口,也就是契約即可。
  • 對可能用到的數據格式都支持,如 Json、text、xml、file 等。
  • 還能與其他工具集成,如 Junit、Maven、Gradle 等。

原理

Moco 本身支持 API 和獨立運行兩種方式。通過 API ,開發人員可以在Junit、TestNg 等測試框架裏使用 Moco,這樣極大地降低了接口測試的複雜度。
Moco 根據一些配置,啓動一個真正的 HTTP 服務(監聽本地指定端口)。當發起的請求滿足一個條件時,就會收到一個 response 。Moco 底層並沒有依賴於像 Servlet 這樣的重型框架,而是基於 Netty 的網絡應用框架編寫的,這樣就繞過了複雜的應用服務器,所以它的速度是極快的。

使用

加載配置啓動 Moco HTTP Server

java -jar <moco-runner-path> http -p <port> -c <configfile-path>

啓動命令參數含義:

  • moco-runner-pathmoco-runner-0.11.0-standalone.jar 包路徑。
  • port :HTTP 服務監聽端口。
  • configfile-path :配置文件路徑

下面介紹不同的 HTTP 服務,以及如何設置 JSON 文件的參數

在本地啓動一個 http 服務器,其中監聽端口是 12306,配置文件是 JSON 文件。只需要本機發起一個request,如:http://localhost:12306

約定請求 URI

JSON 腳本

[
  {
    "description":"這是一個請求URI",
    "request":{
      "uri":"/7d"
    },
    "response":{
      "text":"success!"
    }
  }
]

啓動命令

java -jar moco-runner-0.11.0-standalone.jar http -p 12306 -c ./src/main/resources/startupURI.json

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 7b5a1a47-a287-4674-b94e-c455fc5c645a
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Queries

JSON 腳本

[
  {
    "description":"這是一個請求queries",
    "request":{
      "uri":"/7d",
      "queries":{
        "name":"zuozewei"
      }
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 2d36e386-e022-4478-8acd-258eff4ff684
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Get 方法

JSON 腳本

[
  {
    "description":"這是一個get請求",
    "request":{
      "uri":"/7d",
      "method":"get"
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: ae3250b6-0ec0-4875-8970-d37e5b840820
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Post 方法

JSON 腳本

[
  {
    "description":"這是一個post請求",
    "request":{
      "uri":"/7d",
      "method":"post"
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO  Request received:

POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 73f38af1-4efb-473a-b9d2-de0392c65bbe
X-Lantern-Version: 5.1.0

09 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Headers

JSON 腳本

[
  {
    "description":"這是一個帶headers的post請求",
    "request":{
      "uri":"/7d",
      "method":"post",
      "headers":{
        "content-type":"application/json"
      }
    },
    "response":{
      "text":"success!"
      }
    }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO  Request received:

POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: application/json
Postman-Token: 0a82d74b-303f-42a3-9da0-32fd6c604166
X-Lantern-Version: 5.1.0

09 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Cookies

JSON 腳本

[
  {
    "description":"這是一個帶cookies的post請求",
    "request":{
      "uri":"/7d",
      "method":"post",
      "cookies":{
        "login":"7dgroup"
      }
    },
    "response":{
      "text":"success!"
      }
    }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO  Request received:

POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Cookie: login=7dgroup
Postman-Token: 36a12412-6eb1-44a4-a2d8-ea222eba8968
X-Lantern-Version: 5.1.0

09 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 Forms

JSON 腳本

[
  {
    "description":"這是一個帶forms參數的post請求",
    "request":{
      "uri":"/7d",
      "method":"post",
      "forms":{
        "name":"zuozewei"
      }
    },
    "response":{
      "text":"success!"
      }
    }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO  Request received:

POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 167
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------977669308391204172275520
Postman-Token: 308d06bf-c110-4736-9ac4-ee2fe8a4a036
X-Lantern-Version: 5.1.0

<content is binary>

09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 URI (Match)

對於 Restful 風格的 url ,支持正則匹配。
JSON 腳本

[
  {
    "description":"這是一個請求Match URI",
    "request":{
      "uri":
          {
            "match":"/\\w*/7d"
          }
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO  Request received:

POST /wzuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------767805110351846142172059
Postman-Token: 5d7b5c65-1f8b-46ae-8868-62def1a5de31
X-Lantern-Version: 5.1.0

09 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 URI (StartsWith)

JSON 腳本

[
  {
    "description":"這是一個請求StartsWith URI",
    "request":{
      "uri":
          {
            "startsWith":"/7d"
          }
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d/zuozewei HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------445269276531904972620891
Postman-Token: f9deca3a-9b59-426c-ad48-00ebb4800321
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 URI (endsWith)

JSON 腳本

[
  {
    "description":"這是一個請求endsWith URI",
    "request":{
      "uri":
          {
            "endsWith":"/7d"
          }
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO  Request received:

GET /zuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------516453569550782372688423
Postman-Token: 774378a6-5e57-4cc2-a015-f4b3bd2cb84d
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定請求 URI (Contain)

JSON 腳本

[
  {
    "description":"這是一個請求Contain URI",
    "request":{
      "uri":
          {
            "contain":"/7d"
          }
    },
    "response":{
      "text":"success!"
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO  Request received:

GET /zuozewei/7d/12345 HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------030965700716204296542028
Postman-Token: 7615db1b-77e1-40f7-bdc3-e464c4e7269a
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8

success!

約定指定 Json 響應

JSON 腳本

[
  {
    "description":"這是一個指定Json響應的post請求",
    "request":{
      "uri":"/7d",
      "method":"post"
    },
    "response":{
      "json":{
        "name":"success",
        "code":"1"
      }
    }
  }
]

通過 Postman 驗證服務,測試 Post 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO  Request received:

POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------703341725381001692596870
Postman-Token: e5686919-85b9-44d0-8a73-61bf804b6377
X-Lantern-Version: 5.1.0

09 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 29
Content-Type: application/json; charset=utf-8

{"name":"success","code":"1"}

約定響應 Status

JSON 腳本

[
  {
    "description":"這是指定響應status的get請求",
    "request":{
      "uri":"/7d",
      "method":"get"
    },
    "response":{
      "status":200
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------465777039297587100709267
Postman-Token: 791fa21c-386f-4389-aaa9-ba06d9e53aff
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200

約定響應 Headers

JSON 腳本

[
  {
    "description":"這是一個get請求",
    "request":{
      "uri":"/7d",
      "method":"get"
    },
    "response":{
      "headers":{
        "content-type":"application/json"
      }
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------774041889819140984857561
Postman-Token: 0a51f958-0338-4afa-8ff6-af45d61e12a7
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
content-type: application/json

約定響應 Cookies

JSON 腳本

[
  {
    "description":"這是一個響應Cookies的get請求",
    "request":{
      "uri":"/7d",
      "method":"get"
    },
    "response":{
      "cookies":{
        "login":"7dgroup"
      }
    }
  }
]

通過 Postman 驗證服務,測試 Get 請求
在這裏插入圖片描述
Moco 服務日誌

09 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------315176206881627055625168
Postman-Token: f6d1ae6b-c1c2-474a-827c-f02ed3f23482
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO  Response return:

HTTP/1.1 200
Set-Cookie: login=7dgroup; Path=/

約定重定向 RedirectTo

JSON 腳本

[
  {
    "description":"這是一個重定向的get請求",
    "request":{
      "uri":"/7d",
      "method":"get"
    },
    "redirectTo":"http://blog.csdn.net/zuozewei"
  }
]

通過瀏覽器驗證服務,測試 Get 請求
http://127.0.0.1:12306/7d

Moco 服務日誌

09 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO  Request received:

GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------167408129884853494096695
Cookie: login=7dgroup
Postman-Token: f83696d4-37ba-45b6-aff6-6f20982673ac
X-Lantern-Version: 5.1.0
Content-Length: 0

09 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO  Response return:

HTTP/1.1 302
Location: http://blog.csdn.net/zuozewei

小結

Moco 的使用很簡單,配置也很方便,目前更是提供了 http、rest、socket 服務。但是也僅僅是能 stub 接口,模擬出簡單的場景。如果接收到請求後需要做一些處理,如需查詢數據庫、進行運算、或者一些複雜的操作,就無能爲力了。所以是否選用 Moco,就取決於測試人員是否只是需要一個簡單的模擬服務器。

本文源碼:https://github.com/zuozewei/moco-demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章