Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

...

StubFactory

...

StubFactory is used to create temporary mocks for front-end and back-end developers before real models and real data is applied.
The feature of the class is ability to generate different instances of simple or complex types.

Overview

StubFactory takes a Class instance and returns an object which fields are filled with appropriate data.

StubFactory can generate:

  • primitives (boolean, short, int and so on)
  • string
  • list
  • map
  • complex object e.g. Person (name, age, birthday...)

StubFactory works recursively, so it can generate collections with other nested types e.g. List<Map<List<Integer>, String>> and so on.

Usage

The basic usage of StubFactory:

Code Block
languagejava
...
StubFactory.produce(new TypeReference<GenericApiResponse<YOUR_CLASS>>(){ });
...

Just replace 'YOUR_CLASS' with an appropriate DTO (usually *Response or *Request classes)

Usually code above is generated automatically during api generation through JBT (see api-generate) but such behavior can be changed. To turn off stub generation <strong>#TODO end this paragraph</strong>

MockDataGenerator

...

Overview

Basically, StubFactory generates random appropriate data, but it also is able to generate meaningful data using MockDataGenerator.
Meaningful data provided by MockDataGenerator is listed below:

  • name (first name + last name) / last name
  • phone number
  • email
  • address
  • date
  • datetime
  • text (like Lorem Ipsum)

Also, MockDataGenerator supports different locales.

Usage

To use generation of a data above stubData attribute with appropriate value should be specified in XML-file during DTO describing.
For example, to get an instance of Person class with fields fullName and birthday with near-to-real values (such as 'Tim Smith' and '1995-02-12') the XML-file must have the following structure:

Code Block
titlepersonResponse.xml
collapsetrue
<class name="PersonResponse" xmlns="http://www.knubisoft.com/api/schema/class">
<string name="fullName" description="Fake full name of a person" stubData="fullName"/>
<string name="birthday" description="Fake birthday of a person" stubData="date"/>
</class>

To generate DTO java class from specification above see api-generate.

Result

Querying an endpoint with a specified DTO may provide the following response:

Code Block
titleresponse.json
collapsetrue
{
"body": {
"fullName": "Lon Wyman",
"birthday": "Fri Apr 09 12:15:47 EEST 1993",
},
"errors": null,
"debugInfo": null
}