https://github.com/eliasnogueira/example-test-data-builder
How to implement and use Test Data Builder
https://github.com/eliasnogueira/example-test-data-builder
data testautomation testing
Last synced: 4 days ago
JSON representation
How to implement and use Test Data Builder
- Host: GitHub
- URL: https://github.com/eliasnogueira/example-test-data-builder
- Owner: eliasnogueira
- License: mit
- Created: 2019-12-19T07:23:39.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-12-25T13:39:58.000Z (almost 2 years ago)
- Last Synced: 2025-09-07T07:20:54.293Z (about 1 month ago)
- Topics: data, testautomation, testing
- Language: Java
- Homepage:
- Size: 24.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# How to create a Test Data Builder
## Environment
* Maven
* Java JDK 21+
* Lombok plugin
* Enable _Annotation Processing_
## Project Structure
The project structure is described in a logical way showing how is the easiest way to create an object with data to use in your test and, in the end, the recommendation using the Test Data Builder.### src/main
#### model/plain
The `UserRegistration` class is a plain object with _getters_ and _setters_, _constructor_ and _toString()_.The `UserRegistrationBuilder` is the Builder pattern applied using the `UserRegistration` class to make easier the object creation.
> :warning: This approach is not recommended but if you're not used to creating Java classes, this might be helpful.
#### model/lombok
To avoid you to write get, setters, toString, and builders you can use Lombok.
On the `UserRegistration` class you can see the `@Data` and `Builder` that, respectively, automatically create the get
and setters, and the builder.> :warning: This approach is not recommended. The only difference between this and the plain approach is that now we are
using less code.### data
This package contains the Test Data Builder classes.
Both classes on this package use `UserRegister` object created with Lombok.The `UserRegistrationFixedData` has all the methods to create different data but with fixed data,
which means your tests will have the same data all the time.The `UserRegistrationData` has all the methods to create different dynamic data. Faker is being used to generate the data.
Even though you use the same method twice in your test, the data will be different.### src/test
#### plain
The class `PlainClassTest` shows how is the process to create a test with data to use in your test **in the regular way**.The class `PlainClassUsingBuilderTest` shows how is the process to create a test with **data using a builder**.
#### lombok
The class `UsingLombokBuilderTest` shows the same approach of the previous one, but using lombok with the builder in order to have less code.#### data
The class `UsingFixedDataTest` shows how is the process to **create a test with the Test Data Builder having fixed data**.This class is being used on `UsingDynamicDataTest` to show how is the process to **create a test with the Test Data Builder having dynamic data**.
### resources
Log4J2 properties files used to show the log information in the console.
Note that Log4J2 is being using directly on test classes `PlainClassTest`, `PlainClassUsingBuilderTest` and `UsingLombokBuilderTest` but when we
start using a concrete example of the Test Data Builder the log is being used on these tests.I created this approach to add the responsibility of the data longing in the right place: the place they are being generated.
## References
* https://martinfowler.com/bliki/ObjectMother.html
* https://reflectoring.io/objectmother-fluent-builder
* https://blog.codeleak.pl/2014/06/test-data-builders-and-object-mother.html