Contact Us 1-800-596-4880

Testing with Environment Properties

Assume that you have the following flow that obtains data from an external API and you need to consume it from a test environment when executing your MUnit tests:

<http:request-config name="HTTP_Request_configuration">
	<http:request-connection host="sampleapi.com/api/v1/" port="80"/>
</http:request-config>
<flow name="requestApiFlow">
	<http:request method="GET" config-ref="HTTP_Request_configuration" path="/employee"/>
</flow>

To consume data from a QA API endpoint when running your MUnit tests:

  1. Extract the HTTP Configuration values into a properties file for each environment where you will execute the validations. Those properties files must go under src/main/resources:

    prod.properties
    http.host=dummy.restapiexample.com/api/v1/
    http.port=80
    qa.properties
    http.host=dummy.qax.restapiexample.com/api/v1/
    http.port=8081

    You can set a properties file for each one of your environments.

  2. Use a configuration-properties global configuration in your configuration file to load environment properties from the previously created file:

     <configuration-properties file="${mule-env}.properties"/>
  3. Replace the HTTP configuration with placeholders matching the variables defined in the properties file:

    <http:request-config name="HTTP_Request_configuration">
    	<http:request-connection host="${http.host}" port="${http.port}"/>
    </http:request-config>
    <flow name="requestApiFlow">
    	<http:request method="GET" config-ref="HTTP_Request_configuration" path="/employee"/>
    </flow>
  4. Define an environment variable in the MUnit Maven plugin:

     <configuration>
         ...
         <environmentVariables>
             <mule-env>qa</mule-env>
         </environmentVariables>
         ...
     </configuration>

    During the execution of the MUnit tests, MUnit loads the properties from the qa properties file and connects to your test environment.