WireMock Cloud
Create dynamic, realistic mock responses with WireMock Cloud’s powerful templating capabilities.
Learn more >
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>
dependencies { implementation 'org.wiremock.extensions:wiremock-faker-extension-standalone:0.2.0'}
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));
When running WireMock standalone, register the extension via the command line:
java -jar wiremock-standalone.jar --extensions org.wiremock.extensions.RandomExtension
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"] }}
stubFor(get(urlEqualTo("/api/user")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "application/json") .withBody("{\n" + " \"firstName\": \"{{random 'Name.first_name'}}\",\n" + " \"lastName\": \"{{random 'Name.last_name'}}\",\n" + " \"email\": \"{{random 'Internet.email_address'}}\"\n" + "}") .withTransformers("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 nameName.last_name
- Last nameName.full_name
- Full nameName.name_with_middle
- Name with middle nameName.prefix
- Name prefix (Mr., Mrs., etc.)Name.suffix
- Name suffix (Jr., Sr., etc.)Internet.email_address
- Email addressInternet.safe_email_address
- Safe email addressPhoneNumber.phone_number
- Phone numberPhoneNumber.cell_phone
- Cell phone numberAddress.street_address
- Street addressAddress.city
- City nameAddress.state
- State nameAddress.state_abbr
- State abbreviationAddress.zip_code
- ZIP codeAddress.country
- Country nameCompany.name
- Company nameCompany.industry
- IndustryJob.title
- Job titleJob.field
- Job fieldCommerce.department
- Department nameCommerce.product_name
- Product nameCommerce.price
- PriceInternet.url
- URLInternet.domain_name
- Domain nameInternet.ip_v4_address
- IPv4 addressInternet.ip_v6_address
- IPv6 addressInternet.mac_address
- MAC addressInternet.uuid
- UUIDDateAndTime.birthday
- BirthdayDateAndTime.future
- Future dateLorem.characters
- Random charactersLorem.word
- Random wordLorem.sentence
- Random sentenceLorem.paragraph
- Random paragraphFood.ingredient
- Food ingredientFood.spice
- Spice nameBeer.name
- Beer nameCoffee.blend_name
- Coffee blendBook.title
- Book titleBook.author
- Book authorMusic.genre
- Music genreMovie.title
- Movie titleFinance.credit_card
- Credit card numberFinance.iban
- IBANFinance.bic
- BIC/SWIFT codeAddress.postcode_by_state.AL
- Postcode for AlabamaFor 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!
Use with response templating: Always enable response templating when using the Faker extension by adding "transformers": ["response-template"]
to your stub.
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}}"}
Choose appropriate types: Select faker types that match your domain. For example, use Job.title
for employee data rather than generic Lorem.word
.
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.