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.
StubFactory takes a Class instance and returns an object which fields are filled with appropriate data.
StubFactory can generate:
StubFactory works recursively, so it can generate collections with other nested types e.g. List<Map<List<Integer>, String>> and so on.
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)
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>
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:
Also, MockDataGenerator supports different locales.
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:
<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.
Querying an endpoint with a specified DTO may provide the following response:
{ "body": { "fullName": "Lon Wyman", "birthday": "Fri Apr 09 12:15:47 EEST 1993", }, "errors": null, "debugInfo": null } |