-
Notifications
You must be signed in to change notification settings - Fork 1
Home
A Java library to create test objects (randomly or based on template)
Have you ever struggle to write some simple test objects in your tests? Me too. With Play-Doh you can generate instantly test objects with random generated data in a blink of an eye.
Testing is good but sometimes it takes a lot of effort to write good tests. Sometimes you need to create constructors for your objects that only tests are going to call. Sometimes you need to use some intrusive libraries as Spring's ReflectionUtils to avoid using those constructors but it feels so dirty.
Even with some cool technics as Test Data Builder sometimes it's so hard to create a simple object with some data just to write a test.
I started writing this library to help me write less while writing tests. I believe that it helped me a lot and I hope that it will help you too.
You have something like this:
public class Dummy {
private Integer integerValue;
private String stringValue;
private NestedDummy nestedDummy;
...
}
You create a Dummy class instance:
Dummy dummy = Playdoh.build(Dummy.class);
And you get this:
dummy.toString()
> Dummy{integerValue=1463994319, stringValue='wtnUOzhmIs', nested=NestedDummy{intValue=1169020145, stringValue='DaVaSeMtrU'}}
If you want to set some of the fields with specific values you should do this:
Dummy dummy = Playdoh.builder(Dummy.class)
.with("stringValue", "test")
.build();
So you will have something like this:
dummy.toString()
> Dummy{integerValue=1463994319, stringValue='test', nested=NestedDummy{intValue=1169020145, stringValue='DaVaSeMtrU'}}
You can also create an object with the data specified in a fixture file (yml file). For example for an User class:
public class User {
private String name;
private String email;
private int age;
...
You must have an user.yml file in your classpath with the data for an user. Like this one:
aUser:
name: Lucas Saldanha
email: [email protected]
age: 123
Then you can create an User with the data as specified in the fixture file:
User user = Playdoh.build(User.class, "aUser");
And you get:
user.toString()
> User{name='Lucas Saldanha', email='[email protected]', age=123}
You may have multiple user data defined in user.yml file. You choose which one will be used with the identifier passed as parameter to the Playdoh.build(TYPE, IDENTIFIER)
method. Your fixture file must follow a snake-case convention. If you have a HelloWorld.class you must name you fixture file hello_world.yml.
In current state if your model object has a interface type field from Java Collections (like Set or Map) if you use Playdoh to generate random data (not from fixtures) it won't fill those fields with random data, it will just create an instance of the Set or Map (using a default implementation for those interfaces).
If you need those fields filled with data I suggest you to use Playdoh with a fixture file to create the object. See Creating objets with fixtures wiki page for usage examples.
You need JDK > 1.6 and Maven to build the sources and run the tests.
To run the tests execute in the project folder:
mvn test
Feel free to colaborate with htis project. Pull requests and issue creation are welcome. With you have any doubts about getting involved get in touch and we can talk.
The MIT License (MIT) Copyright (c) 2015 Lucas Saldanha
For more details check the LICENCE file in the project.