Code Monkey home page Code Monkey logo

fixture-factory's Introduction

Fixture Factory - generator to create fake objects from a template

Build Status

Installing

Use it like a maven dependency on your project

<dependency>
	<groupId>br.com.six2six</groupId>
	<artifactId>fixture-factory</artifactId>
	<version>2.1.0</version>
</dependency>

Usage

Writing bean template rules

Fixture.of(Client.class).addTemplate("valid", new Rule(){{
	add("id", random(Long.class, range(1L, 200L)));
	add("name", random("Anderson Parra", "Arthur Hirata"));
	add("nickname", random("nerd", "geek"));
	add("email", "${nickname}@gmail.com");
	add("birthday", instant("18 years ago"));
	add("address", fixture(Address.class, "valid"));
}});

Fixture.of(Address.class).addTemplate("valid", new Rule(){{
	add("id", random(Long.class, range(1L, 100L)));
	add("street", random("Paulista Avenue", "Ibirapuera Avenue"));
	add("city", "São Paulo");
	add("state", "${city}");
	add("country", "Brazil");
	add("zipCode", random("06608000", "17720000"));
}});

Creating a new template based on another existing template. Using this you can override the definition for a property

Fixture.of(Address.class).addTemplate("valid-augusta").inherits("valid", new Rule(){{
	add("street", "Augusta Street");
}});

Gimme one object from valid label

Client client = Fixture.from(Client.class).gimme("valid");

Gimme N objects from valid label

List<Client> clients = Fixture.from(Client.class).gimme(5, "valid");

More helpers functions for create generic template:

Relationship (one-to-one and one-to-many)

Fixture.of(Order.class).addTemplate("valid", new Rule(){{
	add("id", random(Long.class, range(1L, 200L)));
	add("items", has(3).of(Item.class, "valid"));
	add("payment", one(Payment.class, "valid"));
}});

Fixture.of(Item.class).addTemplate("valid", new Rule(){{
	add("productId", random(Integer.class, range(1L, 200L)));
}});

Fixture.of(Payment.class).addTemplate("valid", new Rule(){{
	add("id", random(Long.class, range(1L, 200L)));
}});

Regex

Fixture.of(Any.class).addTemplate("valid", new Rule(){{
  add("id", regex("\\d{3,5}"));
  add("phoneNumber", regex("(\\d{2})-(\\d{4})-(\\d{4})"));
});

Date

Fixture.of(Any.class).addTemplate("valid", new Rule(){{
	add("dueDate", beforeDate("2011-04-15", new SimpleDateFormat("yyyy-MM-dd")));
	add("payDate", afterDate("2011-04-15", new SimpleDateFormat("yyyy-MM-dd")));
	add("birthday", randomDate("2011-04-15", "2011-11-07", new SimpleDateFormat("yyyy-MM-dd")));
	add("cutDate", instant("now"));
});

Name

Fixture.of(Any.class).addTemplate("valid", new Rule(){{
	add("firstName", firstName());
	add("lastName", lastName());
});

You can see more utilization on tests!

Contributing

Want to contribute with code, documentation or bug report?

Do this by joining the mailing list on Google Groups.

Credits

Fixture-Factory was written by:

with contributions from several authors, including:

License

Fixture-Factory is released under the Apache 2.0 license. See the LICENSE file included with the distribution for details.

fixture-factory's People

Contributors

nykolaslima avatar aparra avatar ahirata avatar leocomelli avatar douglasrodrigo avatar marcelocenerine avatar qmx avatar thiagocaiubi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.