Contact Us 1-800-596-4880

Parameterized

logo acb active Anypoint Code Builder

logo studio active Anypoint Studio

A parameterized test suite runs the same tests but with different inputs.

The test suite parameterization is defined at a configuration level as follows:

<munit:config>
    <munit:parameterizations>
        <munit:parameterization name="firstParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="Robert"/>
                <munit:parameter propertyName="lastname" value="Plant"/>
            </munit:parameters>
        </munit:parameterization>
        <munit:parameterization name="secondParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="Jimmy"/>
                <munit:parameter propertyName="lastname" value="Page"/>
            </munit:parameters>
        </munit:parameterization>
    </munit:parameterizations>
</munit:config>

The Test Suite runs twice: Firstly, with the firstParameterization parameters. Secondly, with the secondParameterization parameters.

For example, if you have a test that sets a payload for a flow and expects a result:

Parameterization Example
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:munit="http://www.mulesoft.org/schema/mule/munit"
      xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
      xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
        http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">

    <munit:config name="parameterized-test.xml">
        <munit:parameterizations>
            <munit:parameterization name="sumThree">
                <munit:parameters>
                    <munit:parameter propertyName="input" value="#[1]"/>
                    <munit:parameter propertyName="output" value="#[2]"/>
                </munit:parameters>
            </munit:parameterization>
            <munit:parameterization name="sumFive">
                <munit:parameters>
                    <munit:parameter propertyName="input" value="#[2]"/>
                    <munit:parameter propertyName="output" value="#[4]"/>
                </munit:parameters>
            </munit:parameterization>
        </munit:parameterizations>
    </munit:config>

    <munit:test name="expectFlowResult">
        <munit:behavior>
            <set-payload value="${input}"/>
        </munit:behavior>
        <munit:execution >
            <flow-ref name="myFlow"/>
        </munit:execution>
        <munit:validation>
            <munit-tools:assert-equals actual="#[payload]" expected="${output}" />
        </munit:validation>
    </munit:test>

</mule>

Access Parameters from DataWeave

Use the DataWeave p() function to access parameter values inside DataWeave expressions, concatenate parameter values with other strings, perform comparisons or calculations, and use parameters in complex DataWeave transformations.

Using the parameterization from the first example, the following test uses p() to build a greeting message and validate that the payload contains the expected name:

Accessing Parameters with the p() Function
<munit:test name="validateGreeting">
    <munit:behavior>
        <set-payload value="#['Hello, ' ++ p('name') ++ ' ' ++ p('lastname')]"/>
    </munit:behavior>
    <munit:validation>
        <munit-tools:assert-that
          expression="#[payload contains p('name')]"
          is="#[MunitTools::equalTo(true)]"/>
    </munit:validation>
</munit:test>

Parameterization from File

To use the same parameterized values on different test suites, you must save your parameterizations values in a YAML file in the /test/resources directory of your Mule application project:

parameterizations.yaml Example
firstParameterization:
    name: "Robert"
    lastname: "Plant"

secondParameterization:
    name: "Jimmy"
    lastname: "Page"

And reference it from your test suite configuration:

Example Parameterization From File
<munit:config>
    <munit:parameterizations file="parameterizations.yaml" />
</munit:config>

You can combine both methods to add parameterized values to a test suite:

Example Parameterization From File and in the Test Suite Configuration
<munit:config name="parameterization-from-file-test.xml">
    <munit:parameterizations file="parameterizations.yaml" >
        <munit:parameterization name="thirdParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="John"/>
                <munit:parameter propertyName="lastname" value="Bonham"/>
            </munit:parameters>
        </munit:parameterization>
    </munit:parameterizations>
</munit:config>

If you define the same parameterization key in a YAML file and your test suite configuration, the test suite parameterization overrides your YAML parameterization.