先後端分離或者是進行單元測試的時候,必需要用mock api替換掉第三方調用或者是實際的API,eolinker提供了很是全面的mock api支持,其餘那些須要安裝配置的mock server就不說了,有現成的幹嗎還要本身搭。html
Api信息以下,url裏面能夠把根路徑去掉,直接寫api的地址就行。到時候在項目裏面統一配置線上環境以及測試環境的地址前綴,切換就好。java
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
在api的編輯頁面,高級mock裏面,輸入mock的規則就行。eolinker的mock是基於mockjs來改的,不過規則大同小異,規則能夠參考這裏http://mockjs.com/examples.html後端
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
編輯完成以後,回到api的詳情頁面,在mock的標籤下面會看到有一個url,複製到項目裏面就行。由於地址前綴是統一的,因此能夠按照剛剛說的,在項目裏統一配置一個測試的前綴,到時候上線就換成生產環境的前綴既可。支持restful這點仍是很是不錯的,彷佛是我目前看到惟一支持restful的mock server?api
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
用到項目裏面大概是這樣的(只是簡單寫了個demo):restful
- public class MockServerTest {
- @Test
- public void testMockServer() throws IOException {
- mockClient.when(
- request()
- .withPath("http://mock.eolinker.com/TPivSvI55cabdf9f2243a023dc6a76ec9514dc38084c19d?uri=/bestedu/course/primarySchool")
- .withMethod("POST")
- // .withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))
- // .withQueryStringParameter(new Parameter("my-token", "12345"))
- ).respond(
- response()
- .withStatusCode(200)
- .withBody(expected)
- );
- CloseableHttpClient client = HttpClients.createDefault();
- HttpGet httpGet = new HttpPost("http://mock.eolinker.com/TPivSvI55cabdf9f2243a023dc6a76ec9514dc38084c19d?uri=/bestedu/course/primarySchool");
- CloseableHttpResponse response = client.execute(httpGet);
- //驗證輸出是不是正確
- InputStream content = response.getEntity().getContent();
- InputStreamReader inputStreamReader = new InputStreamReader(content);
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
- String responseText = bufferedReader.readLine();
- assertThat(responseText, equalTo(expected));
- }
- }
除了返回mock數據以外,eolinker還有對請求方式以及參數的校驗,若是不對的話還會提示,這個仍是很cool的。but,對於mock的校驗只有企業版纔有,默默但願到時候技術老大能夠批一下。。。前後端分離
總結:單元測試
不重複造輪子,如今不少人崇尚本身去搞一套服務啊啥的,研究一下還行,真的幹活仍是簡單粗暴到手即用最好。況且如今也在推崇遠程辦公,eolinker在這一塊我以爲作的仍是ok的。測試