---
schema_version: "1"
id: d94144bf7cb1f6b7796e0797e4bae0188ea21f1c9e89f1c7dacaf2f260e77e2a
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/2.3/utility-classes-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/2.3/utility-classes-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f9617ee3687d22e6b31b4307007a42cf8d2339a27c8001a3a264fbd95fbd1987
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: MUnit
version: 2.3
---

# Using Utility Classes From a Test

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

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>
    ```
