To Implement and Test an API --------實現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可以爲你的應用程序創建單元測試集。假如你參照上一節的指導設置了JSONPlaceholder RAML定義,那麼接下來你可以通過Anypoint Platform爲你在Anypoint Studio上的JSONPlaceholder API來創建一個Stuido項目。使用APIKit來處理既存的基於RAML的API。

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

點擊 文件 新建 Mule項目來創建項目,在新項目嚮導中進行設置。

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.

會出出現瀏覽API Manager中的API的對話框。

Click Add Credentials.

點擊添加驗證信息。

The Anypoint Platform Sign In dialog appears.

出現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機構(比如企業)所屬的API的名字會版本號會出現。

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文件會出現包瀏覽器的src/main/app目錄。在繪圖界面最上面會出現api-main flow,其中包含了APIkit路由。在main flow的下面,是根據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.

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

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.

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

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狀態以及數據體。

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的應用程序通過使用集成的自動化測試創建工具構建必要的測試。

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創建一個測試,來驗證每一個flow是否返回預期的HTTP狀態碼和數據體。

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

輸入站點網址

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

創建一個MUuit測試集

To generate a test suite:

創建一個測試集:

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創建placeholder.xml測試集

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

MUnit爲你的應用中的每一個flow創建測試,並生成一個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所公開的訪問節點返回的HTTP狀態碼和數據體。你所測試的是:

A GET request receives a 200 status code response.

GET請求接收到HTTP200相應

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

相應中的數據體是你所期望的

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測試集
鏈接:https://www.jianshu.com/p/a2e42b7d6291
 

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