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:

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

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

Usage in JBT

Usually StubFactory is used during API generation through JBT. It produces interfaces in com.knubisoft.api.spec with or without default implementation with StubFactory.

To generate API layer without StubFactory (usual interfaces) use the following command from the project's root directory:

cmd/app generate api


To generate API layer with StubFactory usage (default method with StubFactory in interfaces) use the following command from the project's root directory:

cmd/app generate api-stub

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:

personResponse.xml
<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:

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



If you have any questions/suggestions/remarks feel free to contact Diakonov Serhii. Hope you'll find it useful =)


  • No labels