How to use Java Faker to generate data in MuleSoft

Zoran Đukić
Another Integration Blog
3 min readJun 15, 2023

Whether you are developing software applications, testing new features, or conducting data analysis, having access to realistic mock data can save you time and resources. This is where Java Faker, a powerful library for generating fake data, comes to the rescue. Also, there is a bonus lesson here on how to invoke static methods.

Java Faker is a popular open-source library that provides developers with a wide range of methods to generate random, yet believable, data. It offers a comprehensive set of data types, including names, addresses, phone numbers, dates, lorem ipsum text, and more. By utilizing Java Faker, you can easily generate vast amounts of synthetic data that closely resemble real-world information.

This blog post aims to explore the capabilities of Java Faker and demonstrate how it can be integrated into your Mule projects. Let’s start by creating the project and adding the dependency to our pom.xml.

<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>

Now we have to write code that will give us a name. Go to your Mule Pallete, drag and drop “Invoke Static”. There you can find “Class”. Click on the green plus icon. Call your class “User” and click ok.

If you go to src/main/java you will see your User class. Open it and add the following code.

package com.test;

import com.github.javafaker.Faker;

public class User {

public static String getName() {

Faker faker = new Faker();

String randomName = faker.name().firstName();

String randomLastname = faker.name().lastName();

return randomName + " " + randomLastname;
}
}

This code will generate a fake name every time your run it. Now go back to your main xml file.

Change your “Invoke Static” to call the class and method you need. We don’t need arguments but we can’t leave it empty so just add an empty json.

If you run this you will get a String with different names. Later on, you can add that String to JSON, etc.

And this is it. Whether you need data in a specific language, want to define custom data sets, or require unique formatting, Java Faker provides you with the flexibility to achieve your desired results. You can find more examples here:

Faker provides a wide range of mock data from actors, pokemons, tv shows, etc. Also, it’s an open source project so feel free to contribute.

Hope you enjoyed this short blog.

--

--