Kasey Normam
Kasey Normam

Reputation: 1

Error Running MUnit Tests in Mulesoft: NoSuchFieldError: JAVA_21

I'm having a problem.

I'm new to the MuleSoft world and I'm creating a Mule Connector for my company. I downloaded Anypoint Studio 7.21 and created a new project with my connector code.

My project runs fine; I can create a new project, use my connector in the flow, and it works well. However, when I right-click on my project and go to MUnit -> Configure MUnit Maven Support, the IDE adds the following lines to my pom.xml:

<properties>
    // other properties
    <munit.version>3.4.0</munit.version>
</properties>

<build>
    <plugins>
        // other plugins
        <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>
                            <goal>coverage-report</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <coverage>
                        <runCoverage>true</runCoverage>
                        <formats>
                            <format>html</format>
                        </formats>
                    </coverage>
                </configuration>
            </plugin>
    </plugins>
</build>

<dependencies>
    // other dependencies
    <dependency>
        <groupId>com.mulesoft.munit</groupId>
        <artifactId>munit-runner</artifactId>
        <version>3.4.0</version>
        <classifier>mule-plugin</classifier>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.mulesoft.munit</groupId>
        <artifactId>munit-tools</artifactId>
        <version>3.4.0</version>
        <classifier>mule-plugin</classifier>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mule.weave</groupId>
        <artifactId>assertions</artifactId>
        <version>1.2.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

I then created an empty test case for my connector, but when I run the test, the IDE shows me this error:

package org.mule.extensions.oldcoats;

import org.mule.functional.junit4.MuleArtifactFunctionalTestCase;

public class OldCoatsTestCase extends MuleArtifactFunctionalTestCase {

}

When I execute the test, the IDE shows me this error:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.881 s <<< FAILURE! -- in org.mule.extensions.oldcoats.OldCoatsTestCase
[ERROR] org.mule.extensions.oldcoats.OldCoatsTestCase.initializationError -- Time elapsed: 0.004 s <<< ERROR!
java.lang.NoSuchFieldError: JAVA_21
        at org.mule.runtime.core.internal.context.DefaultMuleContext.configureUnsupportedExtensionsClientRunAsync(DefaultMuleContext.java:1602)
        at org.mule.runtime.core.internal.context.DefaultMuleContext.<clinit>(DefaultMuleContext.java:367)
        at org.mule.test.runner.api.ExtensionPluginMetadataGenerator.createExtensionManager(ExtensionPluginMetadataGenerator.java:114)
        at org.mule.test.runner.api.ExtensionPluginMetadataGenerator.<init>(ExtensionPluginMetadataGenerator.java:83)
        at org.mule.test.runner.api.ExtensionPluginMetadataGenerator.<init>(ExtensionPluginMetadataGenerator.java:79)
        at org.mule.test.runner.api.AetherClassPathClassifier.buildPluginUrlClassifications(AetherClassPathClassifier.java:654)
        at org.mule.test.runner.api.AetherClassPathClassifier.classify(AetherClassPathClassifier.java:210)
        at org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder.build(ArtifactIsolatedClassLoaderBuilder.java:301)
        at org.mule.test.runner.ArtifactClassLoaderRunner.createClassLoaderTestRunner(ArtifactClassLoaderRunner.java:295)
        at org.mule.test.runner.ArtifactClassLoaderRunner.<init>(ArtifactClassLoaderRunner.java:147)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:107)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
        at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
        at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:314)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:240)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:214)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:155)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
        at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   OldCoatsTestCase.initializationError » NoSuchField JAVA_21
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

The problem is, I'm not using JAVA_21 in my code, and I don't know what this JAVA_21 is. My project is using Java 17, and I'm using Anypoint Studio 7.21.

This is my JAVA_HOME:

openjdk version "17.0.13" 2024-10-15
OpenJDK Runtime Environment Temurin-17.0.13+11 (build 17.0.13+11)
OpenJDK 64-Bit Server VM Temurin-17.0.13+11 (build 17.0.13+11, mixed mode, sharing)

And this is my maven version:

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: C:\ProgramData\chocolatey\lib\maven\apache-maven-3.9.9
Java version: 17.0.13, vendor: Eclipse Adoptium, runtime: C:\Program Files\Eclipse Adoptium\jdk-17.0.13.11-hotspot

I don't know what is happening, and I don't know how to fix this error. I tried to downgrade the MUnit version to 3.3.0, but the error persists.

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.oldcoats.third</groupId>
    <artifactId>oldcoats-generic-connector</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>mule-extension</packaging>

    <name>oldcoats-generic-connector</name>

    <parent>
        <groupId>org.mule.extensions</groupId>
        <artifactId>mule-modules-parent</artifactId>
        <version>1.8.3</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <app.runtime>4.9.0</app.runtime>
        <mule.maven.plugin.version>4.3.0</mule.maven.plugin.version>
        <munit.version>3.4.0</munit.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-maven-plugin</artifactId>
                <version>${mule.maven.plugin.version}</version>
                <extensions>true</extensions>
            </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>
                            <goal>coverage-report</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <coverage>
                        <runCoverage>true</runCoverage>
                        <formats>
                            <format>html</format>
                        </formats>
                    </coverage>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-http-connector</artifactId>
            <version>1.10.3</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-sockets-connector</artifactId>
            <version>1.2.5</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>com.mulesoft.munit</groupId>
            <artifactId>munit-runner</artifactId>
            <version>3.4.0</version>
            <classifier>mule-plugin</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mulesoft.munit</groupId>
            <artifactId>munit-tools</artifactId>
            <version>3.4.0</version>
            <classifier>mule-plugin</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mule.weave</groupId>
            <artifactId>assertions</artifactId>
            <version>1.2.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>anypoint-exchange-v3</id>
            <name>Anypoint Exchange</name>
            <url>https://maven.anypoint.mulesoft.com/api/v3/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 Releases Repository</name>
            <layout>default</layout>
            <url>https://repository.mulesoft.org/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

This is my mule-artifact.json file:

{
  "minMuleVersion": "4.9.0",
  "javaSpecificationVersions": [
    "17"
  ]
}

And this is my Module class:

package com.oldcoats.mule.internal.modules;

import com.oldcoats.mule.configurations.OldCoatsConfigurationBase;
import com.oldcoats.mule.errors.OldCoatsErrorBase;

import org.mule.runtime.api.meta.Category;
import org.mule.runtime.extension.api.annotation.Configurations;
import org.mule.runtime.extension.api.annotation.Extension;
import org.mule.runtime.extension.api.annotation.dsl.xml.Xml;
import org.mule.runtime.extension.api.annotation.error.ErrorTypes;

import org.mule.sdk.api.annotation.JavaVersionSupport;
import org.mule.sdk.api.meta.JavaVersion;

@Xml(prefix = "oldcoats")
@Extension(name = "OldCoatsConnector", vendor = "OldCoats", category = Category.COMMUNITY)
@JavaVersionSupport({JavaVersion.JAVA_8, JavaVersion.JAVA_11, JavaVersion.JAVA_17})
@Configurations({OldCoatsConfigurationBase.class})
@ErrorTypes(OldCoatsErrorBase.class)
public class OldCoatsModule {

}

All the versions in the pom.xml are added automatically by the IDE when I click on MUnit -> Configure MUnit Maven Support or when I create the project.

I don't know what is happening, and I don't know how to fix this error.

Also, I'm not sure if is a problem in my machine, because the pipeline also fails with the same error. This is the code to run the pipeline:

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: Maven@4
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.17'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'

Can someone help me, please?

Upvotes: 0

Views: 108

Answers (1)

aled
aled

Reputation: 25837

The error happens because you are using dependencies for Mule applications in a Mule connector project.

MUnit is not designed to be used to test connectors projects. It is a testing framework for applications. For unit testing a connector you use MuleArtifactFunctionalTestCase and JUnit, as the test class in the question.

Also other connectors should not be used as dependencies in a Mule Java connector project. Those are also meant to be dependencies for Mule applications, like your own custom connector.

Upvotes: 0

Related Questions