Read Module
RECORDO
Last updated
RECORDO
Last updated
The Recordo Read module is designed to enhance the testing experience in JUnit. This module introduces flexible and convenient ways to create test objects by seamlessly integrating external JSON data.
If the specified JSON file is missing, Recordo will automatically create a new file with an object structure and populate it with random values. This feature streamlines test preparation, allowing developers to focus on establishing anticipated values without the need for manual file creation.
The module provides three convenient approaches for reading objects:
Interface-based Object Factory
The @Read
annotation has two parameters:
Name | Type | Description |
---|---|---|
Developers can define interfaces with the @RecordoObjectFactory
annotation to instruct Recordo on how to create objects. Here's an example interface:
The Interface-based Object Factories feature is built upon the Cariochi Objecto library, inheriting all the capabilities of Objecto while expanding its functionality. Additionally, it introduces the usage of the @Read
annotation for factory methods, altering the behavior of the method by transforming it from generating a random object on each invocation to generating it only upon the first usage and subsequently persisting it to a file.
For detailed information and documentation regarding the Objecto library, please refer to the Objecto documentation.
Factory Methods:
Methods annotated with @Read
represent factory methods for creating objects, specifying the JSON file paths and parameters.
Return Type: The factory method's return type should be the object you want to create.
@Read Annotation: Apply the @Read
annotation to specify the path to the JSON file containing the object's data.
Parameters: If the object creation involves parameters, use method parameters annotated with @Modifier
to specify the paths within the object that need to be modified.
Modification Methods:
Methods annotated with @Modifier
serve as modification methods, allowing developers to alter the factory object with specific parameters.
Return Type: The return type should be the same factory interface (LogRecordObjectFactory
in this case).
@Modifier Annotation: Use the @Modifier
annotation on methods to specify the paths within the object that need to be modified.
Developers can use the ObjectFactory
class along with the @Read
annotation to create objects with specified parameters.
@Read Annotation: Annotate a field of type ObjectFactory<T>
with the @Read
annotation, where T
is the type of object to be created.
ObjectFactory Methods:
create(): Create an object using the default values specified in the associated JSON file.
with(String key, Object value): Specify a key-value pair to modify the object being created. The key uses a JSONPath-like syntax.
Among its features, the module offers a straightforward method for creating objects directly within test methods using the @Read
annotation.
Developers can use the @Read
annotation on test methods to effortlessly create objects with default values, simplifying the testing process. This method is suitable for scenarios where customization or modification of object values is not required.
In the given examples, the test methods effortlessly create objects using the data from the JSON file. Developers can focus on writing test logic without additional setup or object instantiation.
@Read Annotation: Apply the @Read
annotation to a method parameter to signify that the associated JSON file should be used to create an object.
Default Object Creation: The @Read
annotation, when applied directly to a test method parameter, signals Recordo to create an object with default values specified in the associated JSON file.
value
String
Path to JSON file
objectMapper
String
Name of ObjectMapper bean or test class field