Contact Us 1-800-596-4880

MUnit Before and After Scopes

logo acb active Anypoint Code Builder

logo studio active Anypoint Studio

MUnit Before and After scopes let you run setup and teardown code at the suite or test level in a MUnit test suite. Use Before scopes to prepare shared state and After scopes to clean it up, so each test starts and ends in a controlled state.

Before Suite Scope

The MUnit Before Suite scope runs once before all tests in the suite.

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 scope runs once after all tests in the suite complete.

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 runs before each individual test in the suite.

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 runs after each individual test in the suite completes.

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.