Skip to content

lucassaldanha/playdoh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Play-Doh

A Java library to create test objects (from fixture files or with random data)

Build Status Coverage Status Maven Central

Maven dependency

<dependency>
    <groupId>com.github.lucassaldanha</groupId>
    <artifactId>playdoh</artifactId>
    <version>1.0.1</version>
</dependency>

Synopsis

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.

Motivation

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.

Code Example

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: lucas@example.com
  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='lucas@example.com', 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.

Limitations

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.

Tests

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

Contributors

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.

License

The MIT License (MIT) Copyright (c) 2015 Lucas Saldanha

For more details check the LICENCE file in the project.

About

A Java library to create test objects (from fixture files or with random data)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages