Mulesoft 简明教程

MuleSoft - Testing with MUnit

我们了解,单元测试是一种通过该测试源代码的各个单元以确定它们是否适合使用的方法。Java 程序员可以使用 Junit 框架来编写测试用例。同样,MuleSoft 也拥有一个名为 MUnit 的框架,它允许我们为我们的 API 和集成编写自动化测试用例。它非常适合持续集成/部署环境。MUnit 框架的最大优势之一是我们可以将其与 Maven 和 Surefire 集成。

We understand unit testing is a method by which individual units of source code can be tested to determine whether they are fit for use or not. Java programmers can use Junit framework to write test cases. Similarly, MuleSoft is also having a framework called MUnit allowing us to write automated test cases for our APIs and integrations. It is a perfect fit for continuous integration/deployment environment. One of the biggest advantages of MUnit framework is that we can integrate it with Maven and Surefire.

Features of MUnit

以下是 Mule MUnit 测试框架的一些非常有用的功能

Following are some of the very useful features of Mule MUnit testing framework −

  1. In MUnit framework, we can create our Mule test by using Mule code as well as Java code.

  2. We can design and test our Mule apps and APIs, either graphically or in XML, within Anypoint Studio.

  3. MUnit allows us to easily integrate the testing into existing CI/CD process.

  4. It provides auto-generated tests and coverage reports; hence the manual work is minimal.

  5. We can also use local DB/FTP/mail servers to make testing more portable through the CI process.

  6. It allows us to enable or disable tests.

  7. We can also extend the MUnit framework with plugins.

  8. It allows us to verify message processor calls.

  9. With the help of MUnit testing framework, we can disable endpoint connectors as well as flow inbound end points.

  10. We can check error reports with Mule stack trace.

Latest Release of Mule MUnit Testing Framework

MUnit 2.1.4 是 Mule MUnit 测试框架的最新版本。它需要以下硬件和软件要求

MUnit 2.1.4 is the latest release of Mule MUnit testing framework. It requires following hardware and software requirements −

  1. MS Windows 8+

  2. Apple Mac OS X 10.10+

  3. Linux

  4. Java 8

  5. Maven 3.3.3, 3.3.9, 3.5.4, 3.6.0

它与 Mule 4.1.4 和 Anypoint Studio 7.3.0 兼容。

It is compatible with Mule 4.1.4 and Anypoint Studio 7.3.0.

MUnit and Anypoint Studio

如上文所讨论的,MUnit 已完全集成在 Anypoint Studio 中,我们可以以图形方式或在 Anypoint Studio 内以 XML 形式设计和测试 Mule 应用程序和 API。换句话说,我们可以使用 Anypoint Studio 的图形界面执行以下操作 -

As discussed, MUnit is fully integrated in Anypoint studio and we can design and test our Mule apps and APIs graphically or in XML within Anypoint studio. In other words, we can use graphical interface of Anypoint Studio to do the following −

  1. For creating and designing MUnit tests

  2. For running our tests

  3. For viewing test results as well as coverage report

  4. For debugging the tests

因此,让我们开始逐一讨论每项任务。

So, let us start discussing each task one by one.

Creating and Designing MUnit Tests

一旦你启动新项目,它就会自动向我们的项目中添加一个新文件夹,即 src/test/munit 。例如,我们启动了一个名为 test_munit 的新 Mule 项目,你可以在下图中看到,它在我们的项目下添加了上述文件夹。

Once you start new project, it will automatically add a new folder namely src/test/munit to our project. For example, we started a new Mule project namely test_munit, you can see in the below image, it adds the above-mentioned folder under our project.

designing munit tests

现在,一旦启动新项目,就有两种基本方法可以在 Anypoint Studio 中创建新的 MUnit 测试 -

Now, once you started new project, there are two basic ways to create a new MUnit test in Anypoint Studio −

  1. By Right-Clicking the Flow − In this method, we need to right-click the specific flow and select MUnit from the drop-down menu.

  2. By Using the Wizard − In this method, we need to use the wizard for creating a test. It allows us to create a test for any flow in the workspace.

我们将使用“右键单击流”的方式为特定流创建测试。

We are going to use ‘Right-click the flow’ way to create a test for specific flow.

首先,我们需要按如下方式在工作空间中创建一个流 -

First, we need to create a flow in the workspace as follows −

test munitflow

现在,右键单击此流并选择 MUnit 为此流创建测试,如下所示 -

Now, right click on this flow and select MUnit to create a test for this flow, as shown below −

select munit

它将创建一个以流所在的 XML 文件命名的新测试套件。在这种情况下, test_munit-test-suite 是新测试套件的名称,如下所示 -

It will create a new test suite named after the XML file where the flow resides. In this case, test_munit-test-suite is the name of the new test suite as shown below −

flow execution

以下是用于上述消息流的 XML 编辑器 -

Following is the XML editor for the above message flow −

xml editor

现在,我们可以通过从 Mule Palette 中拖动 MUnit 消息处理器将其添加到测试套件中。

Now, we can add an MUnit message processor to the test suite by dragging it from Mule Palette.

validation assert

如果你想通过向导创建测试,请按照 File → New → MUnit 操作,它将引导你进入以下 MUnit 测试套件 -

If you want to create a test via the Wizard then follow File → New → MUnit and it will lead you to the following MUnit testing suite −

munit test suite

Configuring the test

在 Mule 4 中,我们有两个新部分,分别是 MUnitMUnit Tools ,它们共同拥有所有 MUnit 消息处理器。你可以在你的 MUnit 测试区域中拖动任何消息处理器。在下图中所示 -

In Mule 4, we have two new sections namely MUnit and MUnit Tools, collectively having all MUnit message processor. You can drag any of the message processor in your MUnit test area. It is shown in the below screenshot −

configuring the test

现在,如果你想在 Anypoint Studio 中编辑你的套装或测试的配置,则需要按照以下步骤操作 −

Now, if you want to edit the configuration for your suit or test in Anypoint Studio, then you need to follow the below steps −

Step 1

Step 1

转到 Package Explorer 并右键单击你的套装或测试的 .xml file 。然后,选择 Properties

Go to the Package Explorer and right click on the .xml file for your suite or test. Then, select the Properties.

Step 2

Step 2

现在,在属性窗口中,我们需要单击 Run/Debug Setting*s. After this click *New

Now, in the Properties window, we need to click Run/Debug Setting*s. After this click *New.

Step 3

Step 3

最后一步,单击 Select Configuration Type 窗口下的 MUnit ,然后单击 OK

In the last step, click MUnit under Select Configuration Type window, and then click OK.

edit launch configuration

Running the Test

我们可以运行测试套件以及测试。首先,我们将看到如何运行测试套件。

We can run a test suite as well as a test. First, we will see how to run a test suite.

Running a Test Suite

对于运行测试套件,右键单击你的测试套件所在的 Mule Canvas 空白部分。它会打开一个下拉菜单。现在,单击 Run MUnit suite ,如下所示 −

For running a test suite, right click on the empty part of the Mule Canvas where your test suite resides. It will open a drop-down menu. Now, click on the Run MUnit suite as shown below −

running a test suite

稍后,我们可以在控制台中看到输出。

Later, we can see the output in the console.

Running a Test

要运行特定测试,我们需要选择特定测试并右键单击它。我们将会得到与运行测试套件时相同的下拉菜单。现在,单击 Run MUnit Test 选项,如下所示 −

To run a specific test, we need to select the specific test and right-click on that. We will get the same drop-down menu as we got while running test suite. Now, click on the Run MUnit Test option as shown below −

running a test

然后可以在控制台中看到输出。

There after the output can be seen in the console.

Viewing and Analyzing Test Result

Anypoint studio 在左侧资源管理器窗格的 MUnit tab 中显示 MUnit 测试结果。你可以找到绿色成功的测试和红色失败的测试,如下所示 −

Anypoint studio displays the MUnit test result in the MUnit tab of the left-hand explorer pane. You can find successful tests in green color and failed tests in red one as shown below −

generate report

我们可以通过查看覆盖率报告来分析我们的测试结果。覆盖率报告的主要功能是提供有关 MUnit 测试集成功执行的 Mule 应用程序的多少的指标。MUnit 覆盖基本上基于执行的 MUnit 消息处理器的数量。MUnit 覆盖报告提供以下指标 −

We can analyze our test result by viewing the coverage report. The main feature of Coverage Report is to provide a metric on how much of a Mule application has been successfully executed by a set of MUnit tests. MUnit coverage is basically based on the amount of MUnit message processors executed. MUnit coverage report provides metric for the following −

  1. Application overall coverage

  2. Resource coverage

  3. Flow coverage

要获得覆盖率报告,我们需要单击 MUnit 选项卡下的“生成报告”,如下所示 −

To get the coverage report, we need to click on ‘Generate Report’ under MUnit tab as shown below −

analyzing test result

Debugging the test

我们可以调试一个测试套件以及一个测试。首先,我们将看到如何调试一个测试套件。

We can debug a test suite as well as a test. First, we will see how to debug a test suite.

Debugging a Test Suite

要调试一个测试套件,右键单击存放你的测试套件的 Mule Canvas 空白部分。它会打开一个下拉菜单。现在,单击 Debug MUnit Suite ,如下面的图像所示 −

For debugging a test suite, right click on the empty part of the Mule Canvas where your test suite resides. It will open a drop-down menu. Now, click on the Debug MUnit Suite as shown in the below image −

debugging the test

然后,我们可以在控制台中看到输出。

Then, we can see the output in the console.

Debugging a Test

要调试一个特定测试,我们需要选择特定测试并右键单击它。我们将会得到与调试测试套件时相同的下拉菜单。现在,单击 Debug MUnit Test 选项。它显示在下面屏幕截图中。

To debug a specific test, we need to select the specific test and right-click on that. We will get the same drop-down menu as we got while debugging test suite. Now, click on the Debug MUnit Test option. It is shown in the below screen shot.

debugging a test