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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值