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测试集

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容