Contact Us 1-800-596-4880

Before and After Scopes

logo acb active Anypoint Code Builder

logo studio active Anypoint Studio

Before Suite Scope

The MUnit Before Suite contains code that is meant to be executed before the whole suite. The Before Suite has one execution rule: Run before all the tests, just once.

For example, suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit Before Suite runs just once, before your four tests.

You can only use one Before Suite scope per MUnit test suite.

Use a Before Suite scope to set up state that every test in the suite shares, such as seeding an in-memory database, starting a server utility, or storing values that the tests read. In the example, the jobtitlelookup.csv file creates the table and its initial contents when the database server starts, and the Before Suite adds one shared row that all tests in the suite query:

Before Suite example
<dbserver:config name="MUnit_DB_Server_Config">
  <dbserver:connection csv="jobtitlelookup.csv" database="DATABASE_NAME"
    connectionStringParameters="MODE=MySQL" />
</dbserver:config>

<munit:before-suite name="before-suite" description="Adds a shared row for the whole suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Support Engineer','10','SUP');" />
</munit:before-suite>

<munit:test name="munit.test" description="Test">
  <!-- Every test in this suite can query the row added by the before-suite -->
</munit:test>
The before-suite scope is located outside the MUnit test, so the insert runs once before the first test instead of once per test.

When you place a before-suite in the Anypoint Studio canvas, the scope locates outside the MUnit test.

After Suite Scope

The MUnit After Suite is a scope. It can contain code that is meant to be executed after the whole suite. The After suite has one execution rule: Run after all the tests, just once.

For instance, let’s suppose you have an MUnit Test Suite File with four tests. The code inside an MUnit After Suite, runs just once, after all your tests.

You can only use one After Suite scope per MUnit test suite.

Use an After Suite scope to tear down whatever the Before Suite created, so the next suite run starts from a clean state. The example removes only the row that the Before Suite added, leaving the contents loaded from the CSV file untouched:

After Suite example
<munit:before-suite name="before.suite" description="Adds a shared row for the whole suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Support Engineer','10','SUP');" />
</munit:before-suite>

<munit:test name="munit.test" description="Test">
  <!-- some more mule code here -->
</munit:test>

<munit:after-suite name="after.suite" description="Removes the data created for the suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="DELETE FROM jobtitlelookup WHERE JOBTITLEID='SUP';" />
</munit:after-suite>
The after-suite scope is located outside the MUnit test, so the cleanup runs once after the last test completes.

When you place an after-suite in the Anypoint Studio canvas, the scope locates outside the MUnit test.

Before Test Scope

The MUnit Before Test Scope contains code that is meant to be executed before each test. Each Before Test scope follows the same execution rule: Run before each test.

For instance, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit Before test runs before each of your four tests; it runs four times.

You can only use one Before Test scope per MUnit test suite.

Use a Before Test scope to set up state that each test needs individually, such as inserting a record, creating a file, or setting a variable that the test consumes. The following example inserts a record and sets the variable that the flow under test reads:

Before Test example
<munit:before-test name="before.test" description="Creates the record each test needs">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Culinary Team Member','10','HIR');" />
  <set-variable variableName="jobid" value="HIR" />
</munit:before-test>

<munit:test name="munit.test" description="Test">
  <munit:execution>
    <flow-ref name="selectFlow" />
  </munit:execution>
  <munit:validation>
    <munit-tools:assert-equals actual="#[vars.job]" expected="Culinary Team Member" />
  </munit:validation>
</munit:test>
The before.test scope is located outside the MUnit test, so each test starts from the same record and variable value without repeating the setup in every test.

When you place a before-test in the Anypoint Studio canvas, the scope locates outside the MUnit test.

After Test Scope

The MUnit After Test Scope contains code that is meant to be executed after each test. Each After Test scope follows the same execution rule: Run after each test.

For instance, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit After Test runs after each of your four tests; it runs four times.

You can only use one After Test scope per MUnit test suite.

Use an After Test scope to undo what the Before Test created, so one test doesn’t leave data behind that affects the next one. The following example deletes the record that the Before Test inserted:

After Test example
<munit:before-test name="before.test" description="Creates the record each test needs">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Culinary Team Member','10','HIR');" />
</munit:before-test>

<munit:test name="munit.test" description="Test">
  <!-- some more mule code here -->
</munit:test>

<munit:after-test name="after.test" description="Removes the record created for the test">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="DELETE FROM jobtitlelookup WHERE JOBTITLEID='HIR';" />
</munit:after-test>
The after.test scope is located outside the MUnit test, so the cleanup runs after each test rather than once after the suite.

When you place an after-test in the Anypoint Studio canvas, the scope locates outside the MUnit test.

Before Suite Reference

Attribute Name Description

name

Defines the name of the Before suite. The name must be unique inside the suite.

description

Describes the actions performed.

After Suite Reference

Attribute Name Description

name

Defines the name of the After suite. The name must be unique inside the suite.

description

Describes the actions performed.

Before Test Reference

Attribute Name Description

name

Defines the name of the Before test. The name must be unique inside the suite.

description

Describes the actions performed.

After Test Reference

Attribute Name Description

name

Defines the name of the After test. The name must be unique inside the suite.

description

Describes the actions performed.