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測試集