Skip to content

Faker Extension

WireMock Cloud

Create dynamic, realistic mock responses with WireMock Cloud’s powerful templating capabilities.
Learn more >

The WireMock Faker Extension uses the Data Faker library to generate random, realistic fake data for use in WireMock response templates. This is particularly useful for creating test data that looks authentic without hardcoding values.

The Faker extension is packaged separately from the core WireMock library. Add it to your project using Maven or Gradle:

<dependency>
<groupId>org.wiremock.extensions</groupId>
<artifactId>wiremock-faker-extension</artifactId>
<version>0.2.0</version>
</dependency>

note

The extension includes net.datafaker:datafaker as a transitive dependency. If you encounter build conflicts with this library, you may need to exclude it from other dependencies in your project.

To use the Faker extension, you need to register it with your WireMock server:

import org.wiremock.extensions.RandomExtension;
WireMockServer wireMockServer = new WireMockServer(
wireMockConfig()
.extensions(RandomExtension.class)
);

Once the extension is registered, you can use the random Handlebars helper in your response templates. The helper takes a single parameter specifying the type of fake data to generate, in the format Category.Key.

{
"request": {
"method": "GET",
"url": "/api/user"
},
"response": {
"status": 200,
"jsonBody": {
"firstName": "{{random 'Name.first_name'}}",
"lastName": "{{random 'Name.last_name'}}",
"email": "{{random 'Internet.email_address'}}"
},
"transformers": ["response-template"]
}
}

Each request will generate different random values, for example:

{
"firstName": "John",
"lastName": "Smith",
"email": "john.smith@example.com"
}
{
"request": {
"method": "GET",
"urlPathPattern": "/api/users/[0-9]+"
},
"response": {
"status": 200,
"jsonBody": {
"id": "{{request.pathSegments.[2]}}",
"firstName": "{{random 'Name.first_name'}}",
"lastName": "{{random 'Name.last_name'}}",
"email": "{{random 'Internet.email_address'}}",
"phone": "{{random 'PhoneNumber.phone_number'}}",
"address": {
"street": "{{random 'Address.street_address'}}",
"city": "{{random 'Address.city'}}",
"state": "{{random 'Address.state'}}",
"zipCode": "{{random 'Address.zip_code'}}"
},
"company": "{{random 'Company.name'}}",
"jobTitle": "{{random 'Job.title'}}"
},
"transformers": ["response-template"]
}
}
{
"request": {
"method": "GET",
"url": "/api/products"
},
"response": {
"status": 200,
"jsonBody": [
{
"id": 1,
"name": "{{random 'Commerce.product_name'}}",
"price": "{{random 'Commerce.price'}}",
"category": "{{random 'Commerce.department'}}",
"color": "{{random 'Color.name'}}",
"material": "{{random 'Commerce.material'}}"
}
],
"transformers": ["response-template"]
}
}

The Faker extension supports a wide variety of data types across multiple categories. Here are some commonly used ones:

  • Name.first_name - First name
  • Name.last_name - Last name
  • Name.full_name - Full name
  • Name.name_with_middle - Name with middle name
  • Name.prefix - Name prefix (Mr., Mrs., etc.)
  • Name.suffix - Name suffix (Jr., Sr., etc.)
  • Internet.email_address - Email address
  • Internet.safe_email_address - Safe email address
  • PhoneNumber.phone_number - Phone number
  • PhoneNumber.cell_phone - Cell phone number
  • Address.street_address - Street address
  • Address.city - City name
  • Address.state - State name
  • Address.state_abbr - State abbreviation
  • Address.zip_code - ZIP code
  • Address.country - Country name
  • Company.name - Company name
  • Company.industry - Industry
  • Job.title - Job title
  • Job.field - Job field
  • Commerce.department - Department name
  • Commerce.product_name - Product name
  • Commerce.price - Price
  • Internet.url - URL
  • Internet.domain_name - Domain name
  • Internet.ip_v4_address - IPv4 address
  • Internet.ip_v6_address - IPv6 address
  • Internet.mac_address - MAC address
  • Internet.uuid - UUID
  • DateAndTime.birthday - Birthday
  • DateAndTime.future - Future date
  • Lorem.characters - Random characters
  • Lorem.word - Random word
  • Lorem.sentence - Random sentence
  • Lorem.paragraph - Random paragraph
  • Food.ingredient - Food ingredient
  • Food.spice - Spice name
  • Beer.name - Beer name
  • Coffee.blend_name - Coffee blend
  • Book.title - Book title
  • Book.author - Book author
  • Music.genre - Music genre
  • Movie.title - Movie title
  • Finance.credit_card - Credit card number
  • Finance.iban - IBAN
  • Finance.bic - BIC/SWIFT code
  • Address.postcode_by_state.AL - Postcode for Alabama
  • (Similar patterns for other US states)

For a complete reference of all available data types, see the Faker Extension Reference Documentation.

By default, the Faker extension generates data in the en-US locale. The generated data will follow American conventions for names, addresses, phone numbers, and other locale-specific formats.

The random helper works seamlessly with other WireMock template helpers:

{{!-- Capitalize a random first name --}}
{{capitalize (random 'Name.first_name')}}
{{!-- Create an email from random name parts --}}
{{toLowerCase (random 'Name.first_name')}}.{{toLowerCase (random 'Name.last_name')}}@example.com
{{!-- Format output with random data --}}
Hello {{random 'Name.first_name'}}, your order #{{randomValue type='NUMERIC' length=8}} has shipped!
  1. Use with response templating: Always enable response templating when using the Faker extension by adding "transformers": ["response-template"] to your stub.

  2. Combine with request data: Mix random data with request parameters for more realistic responses:

    {
    "userId": "{{request.pathSegments.[2]}}",
    "userName": "{{random 'Name.full_name'}}",
    "timestamp": "{{now}}"
    }
  3. Choose appropriate types: Select faker types that match your domain. For example, use Job.title for employee data rather than generic Lorem.word.

  4. Test data consistency: Remember that the faker generates new random values on each request. If you need consistent data across requests, consider using stateful behaviour or storing values in scenarios.