A generic fake factory for Java/Kotlin POJO/POKO.
This library can create an instance of your classes. The idea behind is to avoid creating instances of DTOs/Entities/Domain objects manually.
The main usecase is in tests. creating objects for tests is usually very time consuming and can deviate our attention from the tests. Changes in our POJO/POKOs will cause refactoring in our tests, even if we are not touching that part. With Faketory you can focus on your test.
Faketory is not a mocking library, because it creates real objects and does not control their behaviour. Also, it can not create abstract classes and interfaces (as they are usually not our real domain objects).
Import the dependency:
testImplementation "com.worldsnas:faketory:$latestVersion"
Start creating objects:
val objectOfYourClass = Faketory.create<YourClass>()
TypeReference<YourClass> reference = new TypeReference<YourClass>() {};
YourClass objectOfYourClass = Faketory.create(reference, config);
For more sample please check test directory.
This library is mainly meant for tests
This library is highly customizable. With the help of the Config object, you can customize every step of the object creation process. The default behavior is configured with static fields and generators to avoid extra object creation and speed up the whole instance creation.
You can override the default behavior in two ways:
- Setting a new
Config
to staticFaketory.defaultConfig
field. You can use Kotlincopy
function to avoid passing all the parameters:
Faketory.defaultConfig = Faketory.defaultConfig.copy(
useDefaultConstructor = false,
setNull = false,
useSubObjectsForSealeds = false
)
This approach is good to setup Faketory once (for example: in @BeforeClass) and run all the tests with it.
- If you want more control for each test, you can pass your new
Config
object to theFaketory.create
function:
val actual = Faketory.create<ClassWithDefault>(
Faketory.defaultConfig.copy(useDefaultConstructor = true)
)
Avoid using heavy and time-consuming generators as they will increase your test time
Python Model Bakery
I'm more than happy to discuss, so:
- Create issues
- Send PRs
Any contribution is more than welcome
Faketory is MIT-licensed.