To Implement and Test an 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.
Click File > New > Mule Project to create a project, and set the field values in the new project wizard:
Type an arbitrary name for the project, for example
Select a Mule runtime version, for example Mule 3.8.0 EE.
Check Add APIkit Components.
In API Definition, browse to and select Anypoint Platform.
The Browse API Manager for APIs dialog appears.
Click Add Credentials.
The Anypoint Platform Sign In dialog appears.
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.
Select the API you want to use in Studio: the placeholder API in this example. Click OK and Finish.
placeholder.xmlfile appears in the
src/main/appfolder of the Package Explorer. The
api-mainflow 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.
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.
The project creation process also adds a reference exception strategy to handle all possible errors.
Select Run Project > myproject from the context menu.
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.
Use the mocking service to simulate calls to the API.
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.
In this particular example, you focus on verifying the status and payloads returned by your APIKit router configuration following your RAML specs.
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 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.
Go to Help > Install New Software…
Set the following properties:
MUnit Update Site
Check Munit and Munit Tools for Mule, and click Next to complete the installation.
After installation, restart Anypoint Studio.
To generate a test suite:
Stop any Mule applications that are running from Studio.
Right-click the APIKit Router >, MUnit > Create placeholder.xml Suite for RAML.
MUnit creates a test for each flow in your application and returns an MUnit test suite.
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:
GETrequest receives a
200status code response.
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.
The result of every test is shown in the MUnit View in Anypoint Studio.