To Implement and Test an API 實現(xiàn)API并進行測試

You can use Anypoint Studio to change, mock, and test an API. MUnit can create a unit testing suite for your application. Assuming you set up the JSONPlaceholder RAML definition according to the instructions in the previous section, you create a Studio project for the example JSONPlaceholder API on Anypoint Platform. You use APIkit to process an existing API backed by RAML.

你可以使用Anypoint Studio來交換、仿真和測試API。MUnit可以為你的應用程序創(chuàng)建單元測試集。假如你參照上一節(jié)的指導設置了JSONPlaceholder RAML定義,那么接下來你可以通過Anypoint Platform為你在Anypoint Studio上的JSONPlaceholder API來創(chuàng)建一個Stuido項目。使用APIKit來處理既存的基于RAML的API。

Click File > New > Mule Project to create a project, and set the field values in the new project wizard:

點擊 文件 新建 Mule項目來創(chuàng)建項目,在新項目向導中進行設置。

Type an arbitrary name for the project, for example myproject.

為項目輸入唯一的名字,比如myproject

Select a Mule runtime version, for example Mule 3.8.0 EE.

選擇Mule runtime的版本,比如Mule 3.8.0 EE。

Check Add APIkit Components.

點擊添加APIkit組件。

In API Definition, browse to and select Anypoint Platform.

在API定義中,找到并選擇Anypoint Platform。

The Browse API Manager for APIs dialog appears.

會出出現(xiàn)瀏覽API Manager中的API的對話框。

Click Add Credentials.

點擊添加驗證信息。

The Anypoint Platform Sign In dialog appears.

出現(xiàn)Anypoint Platform登入對話框

Enter your user name and password, and click Sign In.

輸入你的用戶名和密碼,點擊登入。

The names and versions of your APIs in your Anypoint Platform organization, for example the Enterprises organization appear.

你的Anypoint Platform機構(比如企業(yè))所屬的API的名字會版本號會出現(xiàn)。

Select the API you want to use in Studio: the placeholder API in this example. Click OK and Finish.

選擇你想要在Studio中使用的API:比如這次我們要用的placeholder API。點擊OK完成。

The placeholder.xml file appears in the src/main/app folder of the Package Explorer. The api-main flow appears at the top of the canvas and contains the APIkit Router. Below the main flow, there are other flows defined by the RAML, one for each resource/method pairing defined within the RAML spec. Do not rename these flows.

placeholder.xml文件會出現(xiàn)包瀏覽器的src/main/app目錄。在繪圖界面最上面會出現(xiàn)api-main flow,其中包含了APIkit路由。在main flow的下面,是根據(jù)RAML中的定義自動生成的相應flow,每一個都來自于RAML中定義的資源/方法對。不要更改這些flow的名字,否則APIKit無法正確生效。

Notice the API Sync view in the Mule palette. In this view, you synchronize your changes with Anypoint Platform and to download all updates from the Anypoint Platform to your Studio instance.

請留意在Mule的工具視圖中的API Sync視圖。在此視圖中,你在Anypoint Platform中的更改會被同步過來,并且所有的Anypoint Platform更新都會被下載帶的Studio實例。

The project creation process also adds a reference exception strategy to handle all possible errors.

項目創(chuàng)建中會添加一個全局異常處理策略的引用用來處理可能的錯誤。

Select Run Project > myproject from the context menu.

從上下文菜單中國選擇 運行項目 myproject

The Console below the canvas shows the output of building and deploying the project. The APIkit Console tab contains a mockup of the API operations identical to the mockup you used earlier in the API Designer.

繪圖界面下的控制臺會顯示構建和部署項目的輸出信息。APIkit控制臺則包含了與你之前用過的API Desiger中完全一樣的API操作仿真。

Use the mocking service to simulate calls to the API.

使用仿真服務來模擬對API的調用。

Using MUnit for Unit Testing

使用MUnit進行單元測試。

A unit test consists of verifying that a single unit of code works as expected. Use MUnit to ensure the core functions of your application behave as you intended. MUnit is the Mule application testing framework that allows you to build automated tests for your integrations and APIs. We recommend integrating MUnit into your continuous deployment environment.

單元測試包含了對單一代碼單元是否實現(xiàn)了預期功能的驗證。使用MUnit來確保你的應用程序的核心功能表現(xiàn)與預期的一致。MUnit是Mule應用程序的測試框架,可以幫助你為你的系統(tǒng)集成與API創(chuàng)建自動化測試。我們推薦將MUnit集成到你的持續(xù)部署環(huán)境中去。

In this particular example, you focus on verifying the status and payloads returned by your APIKit router configuration following your RAML specs.

在本例中,你專注在驗證由遵照RAML定義而來APIKit路由配置所返回的HTTP狀態(tài)以及數(shù)據(jù)體。

Later on, you can choose to automate each test to run before you deploy the application to guarantee the app works according to your design.

接下來,選擇進行自動化的測試,在你部署程序之前保證你的程序如你所設計的那樣運作。

After you install MUnit as indicated below, work with its integrated automatic test creator to build the necessary tests for a RAML-based application.

在按照下面的說明安裝了MUnit之后,為基于RAML的應用程序通過使用集成的自動化測試創(chuàng)建工具構建必要的測試。

Install MUnit

安裝MUnit

MUnit comes bundled with Studio 5.4.0 and later. If you are running an older Studio version, you first need to install MUnit. Using MUnit you can design your tests the same way you design a Mule application in Studio. You to automatically generate a test suite based on the RAML definition used by your APIKit router. In this case, MUnit creates a test for every flow created in your Mule application with the purpose of verifying the expected http status code and response payload of every flow.

MUnit被包含在Studio 5.4.0及以后的版本中。如果你使用在此之前版本的Studio,你需要單獨安裝MUnit。使用MUnit你可以像設計Mule應用程序那樣的設計你的測試。可以通過APIKit路由中使用的RAML定義來自動生成測試集,MUnit會為你的Mule應用程序中的每一個flow創(chuàng)建一個測試,來驗證每一個flow是否返回預期的HTTP狀態(tài)碼和數(shù)據(jù)體。

Go to Help > Install New Software…?

前往 幫助 安裝新軟件…?

Click Add…?

點擊 添加…?

Set the following properties:

設置下列屬性:

Name to MUnit Update Site

MUnit更新站點的名字

Location to http://studio.mulesoft.org/beta/r5/munit

輸入站點網(wǎng)址

Check Munit and Munit Tools for Mule, and click Next to complete the installation.

選擇Munit以及Mule Munit工具,點擊下一步完成安裝。

After installation, restart Anypoint Studio.

安裝完成,重啟Anypoint Stuido、

Generate an MUnit Test Suite

創(chuàng)建一個MUuit測試集

To generate a test suite:

創(chuàng)建一個測試集:

Stop any Mule applications that are running from Studio.

在Stuido停止所有運行中的Munit應用

Right-click the APIKit Router >, MUnit > Create placeholder.xml Suite for RAML.

右擊 APIkit 路由 MUnit 為RAML創(chuàng)建placeholder.xml測試集

MUnit creates a test for each flow in your application and returns an MUnit test suite.

MUnit為你的應用中的每一個flow創(chuàng)建測試,并生成一個MUnit測試集。

Perform a Test

進行測試

It is important to define the purpose of your test. This automatic test validates the payloads and http response codes returned by your exposed APIKit endpoint. You test for the following things:

定義你測試目的很重要。自動測試驗證你的APIKit所公開的訪問節(jié)點返回的HTTP狀態(tài)碼和數(shù)據(jù)體。你所測試的是:

A GET request receives a 200 status code response.

GET請求接收到HTTP200相應

The payload of this response is the one you are expecting.

相應中的數(shù)據(jù)體是你所期望的

To test this application, right click any blank space in your Test Suite workspace and select Run MUnit Suite.

在測試集工作空間的任意空白處右鍵點擊,選擇運行MUnit測試集。

The result of every test is shown in the MUnit View in Anypoint Studio.

每一個測試的結構都會被顯示在Anypoint Stuido的MUnit視圖中。

See Also

參見

Sync an API

同步一個API

Reference Exception Strategy

異常處理策略引用

APIkit Router

APIkit路由

MUnit

MUnit test suite.

MUnit測試集

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,923評論 6 535
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,740評論 3 420
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,856評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,175評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,931評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,321評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,383評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,533評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,082評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,891評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,618評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,319評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,732評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,987評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,794評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,076評論 2 375

推薦閱讀更多精彩內容