Search code examples
mavenmulesoftmunitmule4

Mule Maven Plugin MUnit Failure: NoSuchElementException


I'm trying to create a build using mule maven plugin, it has to execute MUnit test cases. I've used a configuration property and it has been configured as app-{mule.env}.yaml While running the app it works fine as I pass the argument mule.env=dev but causes failure while creating maven build using mule maven plugin below is POM structure

MUnit specific config:

<configuration>
                         <environmentVariables>
                            <mule.env>dev</mule.env>
                            <mule.key>23454646576575675</mule.key>
                        </environmentVariables>
                      <coverage>
                          <runCoverage>true</runCoverage>
                          <failBuild>true</failBuild>
                          <requiredApplicationCoverage>${app.coverage}</requiredApplicationCoverage>
                          <requiredResourceCoverage>0</requiredResourceCoverage>
                          <requiredFlowCoverage>0</requiredFlowCoverage>
                          <formats>
                              <format>console</format>
                              <format>html</format>
                          </formats>
                      </coverage>
                    </configuration>

Complete POM

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myapp</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>mule-application</packaging>

    <name>app</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <mule.version>4.2.1</mule.version>
        <munit.version>2.2.1</munit.version>
        <app.runtime>4.2.1</app.runtime>
        <mule.maven.plugin.version>3.2.7</mule.maven.plugin.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-maven-plugin</artifactId>
                <version>${mule.maven.plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <cloudHubDeployment>
                        <server>anypoint-platform</server>
                        <environment>${environment}</environment>
                        <applicationName>${application.name}</applicationName>
                        <muleVersion>${mule.version}</muleVersion>
                        <businessGroup>${business.group}</businessGroup>
                        <properties>
                         <mule.env>${mule.env}</mule.env>
                         <mule.key>${mule.key}</mule.key>
                         <anypoint.platform.analytics_base_uri>https://analytics-ingest.anypoint.mulesoft.com</anypoint.platform.analytics_base_uri>
                         <anypoint.platform.client_id>${client-id}</anypoint.platform.client_id>
                         <anypoint.platform.client_secret>${client-secret}</anypoint.platform.client_secret>
                         <anypoint.platform.base_uri>https://anypoint.mulesoft.com</anypoint.platform.base_uri>
                        </properties>
                    </cloudHubDeployment>
                </configuration>
            </plugin>
                <plugin>
                    <groupId>com.mulesoft.munit.tools</groupId>
                    <artifactId>munit-maven-plugin</artifactId>
                    <version>${munit.version}</version>
                    <executions>
                        <execution>
                            <id>test</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                         <environmentVariables>
                            <mule.env>dev</mule.env>
                            <mule.key>23454646576575675</mule.key>
                        </environmentVariables>
                      <coverage>
                          <runCoverage>true</runCoverage>
                          <failBuild>true</failBuild>
                          <requiredApplicationCoverage>${app.coverage}</requiredApplicationCoverage>
                          <requiredResourceCoverage>0</requiredResourceCoverage>
                          <requiredFlowCoverage>0</requiredFlowCoverage>
                          <formats>
                              <format>console</format>
                              <format>html</format>
                          </formats>
                      </coverage>
                    </configuration>
                </plugin>
        </plugins>

    </build>

    <dependencies>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-http-connector</artifactId>
            <version>1.4.1</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-sockets-connector</artifactId>
            <version>1.1.2</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>com.mulesoft.modules</groupId>
            <artifactId>mule-secure-configuration-property-module</artifactId>
            <version>1.0.2</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        
        <dependency>
            <groupId>com.mulesoft.munit</groupId>
            <artifactId>munit-runner</artifactId>
            <version>2.1.5</version>
            <classifier>mule-plugin</classifier>
            <scope>test</scope>
        </dependency>   <dependency>
            <groupId>com.mulesoft.munit</groupId>
            <artifactId>munit-tools</artifactId>
            <version>2.1.5</version>
            <classifier>mule-plugin</classifier>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-jms-connector</artifactId>
            <version>1.3.6</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>com.mulesoft.connectors</groupId>
            <artifactId>anypoint-mq-connector</artifactId>
            <version>3.1.0</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-apikit-module</artifactId>
            <version>1.1.9</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        
    </dependencies>

    <repositories>
        <repository>
            <id>anypoint-exchange</id>
            <name>Anypoint Exchange</name>
            <url>https://maven.anypoint.mulesoft.com/api/v1/maven</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Releases Repository</name>
            <url>https://repository.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mulesoft-releases</id>
            <name>mulesoft release repository</name>
            <layout>default</layout>
            <url>https://repository.mulesoft.org/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

When I try a maven build it gives the following error

INFO  2020-01-02 13:43:20,651 [[MuleRuntime].io.03: [my-app].io@org.mule.munit.runner.remote.api.server.RunnerServer.initialise:154 @6bc6d88] org.mule.munit.runner.remote.api.server.RunnerServer: Waiting for client connection
INFO  2020-01-02 13:43:21,155 [[MuleRuntime].io.03: [my-app].io@org.mule.munit.runner.remote.api.server.RunnerServer.initialise:154 @6bc6d88] org.mule.munit.runner.remote.api.server.RunnerServer: Client connection received from 127.0.0.1 - true
INFO  2020-01-02 13:43:21,564 [[MuleRuntime].io.03: [my-app].io@org.mule.munit.runner.remote.api.server.RunnerServer.initialise:154 @6bc6d88] org.mule.runtime.api.message.AbstractMuleMessageBuilderFactory: Loaded MuleMessageBuilderFactory implementation 'org.mule.runtime.core.internal.message.DefaultMessageBuilderFactory' from classloader 'java.net.URLClassLoader@5177e388'
org.mule.runtime.api.exception.MuleRuntimeException: java.util.NoSuchElementException: No value present
Caused by: java.util.NoSuchElementException: No value present
        at java.util.Optional.get(Optional.java:135)
        at org.mule.runtime.config.internal.LazyMuleArtifactContext.lambda$null$15(LazyMuleArtifactContext.java:278)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.mule.runtime.config.internal.LazyMuleArtifactContext.lambda$createComponents$16(LazyMuleArtifactContext.java:278)
        at org.mule.runtime.core.api.util.ClassUtils.lambda$withContextClassLoader$10(ClassUtils.java:794)
        at org.mule.runtime.core.api.util.ExceptionUtils.tryExpecting(ExceptionUtils.java:227)
        at org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader(ClassUtils.java:849)
        at org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader(ClassUtils.java:813)
        at org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader(ClassUtils.java:793)
        at org.mule.runtime.config.internal.LazyMuleArtifactContext.createComponents(LazyMuleArtifactContext.java:225)

app-dev.yaml

anypoint:
  mq:
    name: "in-queue"
    url: https://mq-us-west-1.anypoint.mulesoft.com/api/v1
    clientId: "![0iop3V64o9uyhffvvn605a9wLr/kuoqX2B6tcMrt2A6MSIPg3f0zR0+W/Cv]"
    clientSecret: "![l0vHmQFIgKjoii8998&*07dsdsfdd4LaMdsfdsEHcxS0]"

The app and its test cases work normally in Anypoint studio but its only failing in maven build.


Solution

  • I've finally understood the reason behind this error, the error message here is vague. The problem lies in defining a global element in the Test suite

    <global-property name="https.port" value="8097"/>
    
    

    As seen above, I tried to override the https port of applications config by using global property which was causing the maven build to fail.

    This works fine when we simply run the Munit test in studio and also overriders the https port to 8097, but fails during maven build.

    Removing the global property resolved the issue.