---
schema_version: "1"
id: d66ba6178f4482e7a5454f8d6c2ffbd7d9fb3476dd9b55c5967bf989cb8a77c1
title: Using Utility Classes From a Test
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/3.6/utility-classes-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/3.6/utility-classes-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d7e944def5066d9e0c3e154892b7ae3036afa169ba35721946cbe72f4cf703a6
description: MuleSoft Documentation Site
release: "3.6"
latest: false
---
---
product: MUnit
version: 3.6
---

# Using Utility Classes From a Test

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

![logo acb active](/mulesoft/current/en-us/_/img/icons/anypoint-code-builder.svg "Anypoint Code Builder")Anypoint Code
Builder

![logo studio active](/mulesoft/current/en-us/_/img/icons/studio.svg "Anypoint Studio")Anypoint Studio

When developing test cases, you might want to use custom Java classes located in `src/test/java`.
You can use Java classes through DataWeave or through Java Module.

When using custom classes, the package of the class must be exported in the `mule-artifact.json`
file:

```json
{
  "minMuleVersion": "4.x.x",
  "classLoaderModelLoaderDescriptor": {
    "id": "mule",
    "attributes": {
      "exportedPackages": ["com.example.test"]
    }
  }
}
```

For example, assume that you want to use in an MUnit test a `MyUtilClass.java` class located in
`src/test/java/com/example/`:

MyUtilClass

```java
package com.example.test;

public class Helper {

  public static long getCurrentMillis() {
    return System.currentTimeMillis();
  }

}
```

## Invoke Your Java Class Using DataWeave

To invoke static methods using DataWeave you must add the `java!` prefix to the package name of the
class:

Use a class in DataWeave

```xml
<munit:test name="usingJavaInDW">
    <munit:behavior>
        <set-variable variableName="number" value="#[java!com::example::test::Helper::currentMillis()]" />
    </munit:behavior>
    <munit:execution>
        <flow-ref name="myFlow"/>
    </munit:execution>
</munit:test>
```

## Invoke Your Java Class Using Java Module

To invoke a custom Java class using Java Module:

1.  Ensure that Java Module is in your project:

    Java Module dependency

    ```xml
    <dependency>
        <groupId>org.mule.module</groupId>
        <artifactId>mule-java-module</artifactId>
        <version>${javaModule.version}</version>
        <classifier>mule-plugin</classifier>
        <scope>test</scope>
    </dependency>
    ```

2.  Use any of the Java Module’s operations.  
    For this example, invoke a static method of a class:

    Use a class with the Java Module

    ```xml
    <munit:test name="usingJavaInDW">
        <munit:behavior>
            <set-variable variableName="number" value="#[java!com::example::test::Helper::currentMillis()]" />
        </munit:behavior>
        <munit:execution>
            <flow-ref name="myFlow"/>
        </munit:execution>
    </munit:test>
    ```
