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.

  1. 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 myproject.

    • 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.

  2. Click Add Credentials.

    The Anypoint Platform Sign In dialog appears.

  3. 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.

  4. Select the API you want to use in Studio: the placeholder API in this example. Click OK and Finish.

    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.

    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.

  5. 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.

Using MUnit for Unit Testing

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.

Install 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.

  1. Go to Help > Install New Software…​

  2. Click Add…​

  3. Set the following properties:

    • Name to MUnit Update Site

    • Location to

  4. Check Munit and Munit Tools for Mule, and click Next to complete the installation.

  5. After installation, restart Anypoint Studio.

Generate an MUnit Test Suite

To generate a test suite:

  1. Stop any Mule applications that are running from Studio.

  2. 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.

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:

  • A GET request receives a 200 status 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.