Contact Us 1-800-596-4880

Configuring the FTP Server Utility in an MUnit Test

logo acb active Anypoint Code Builder

logo studio active Anypoint Studio

If you want to test the following Mule application:

<sftp:config name="SFTP_Config">
    <sftp:connection host="${ftp.host}" port="${ftp.port}" username="${ftp.user}" password="${ftp.password}"/>
</sftp:config>

<configuration-properties file="ftp.properties" />

<flow name="listFlow">
    <sftp:list config-ref="SFTP_Config" directoryPath="."/>
    <foreach>
        <logger level="INFO" message="#[attributes.fileName]"/>
    </foreach>
</flow>

The ftp.properties in src/main/resources has the following content:

ftp.host=localhost
ftp.port=22
ftp.user=max
ftp.password=munittest

The username field can’t be empty. If you don’t have a username-password pair, set the username to anonymous and don’t set any password. By default, the anonymous attribute is set to true.

Install the FTP Server

To install the FTP server:

  1. Look for the MUnit Utils FTP Server module in Exchange.

  2. Add the module to your project:

    <!-- Ftp Server Dependency -->
    <dependency>
        <groupId>com.mulesoft.munit.utils</groupId>
        <artifactId>munit-ftpserver-module</artifactId>
        <version>2.0.1</version>
        <classifier>mule-plugin</classifier>
        <scope>test</scope>
    </dependency>

    The MUnit FTP server artifact in your POM file must have the test scope.

Define the FTP Server

To define the FTP server, change the FTP server attributes as follows:

<ftpserver:config name="MUnit_FTP_Server_Config">
    <ftpserver:connection port="${ftp.port}" username="${ftp.user}" password="${ftp.password}" secure="true" homeDir="${app.home}"/>
----
</ftpserver:config>
----

Run the Test

After configuring the FTP server, you can run the test:

<munit:test name="listFlowTest" description="Test listFlow" >
    <munit:execution>
        <flow-ref name="listFlow"/>
    </munit:execution>
    <munit:validation>
        <munit-tools:assert-that expression="#[sizeOf(payload)]" is="#[MunitTools::greaterThan(0)]"/>
    </munit:validation>
</munit:test>

This FTP accepts any user, so you must not set up a user database or list.

See Also