All Projects → j-easy → Easy Random

j-easy / Easy Random

Licence: mit
The simple, stupid random Java beans/records generator

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Easy Random

Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (-39.82%)
Mutual labels:  random-generation, random
Randomkit
Random data generation in Swift
Stars: ✭ 1,458 (+33.15%)
Mutual labels:  random-generation, random
Random compat
PHP 5.x support for random_bytes() and random_int()
Stars: ✭ 7,950 (+626.03%)
Mutual labels:  random-generation, random
Generatedata
A powerful, feature-rich, random test data generator.
Stars: ✭ 1,883 (+71.96%)
Mutual labels:  random-generation, random
datagen
Java lib that generates random data (numbers, strings, dates) - mostly to facilitate Randomized Testing.
Stars: ✭ 56 (-94.89%)
Mutual labels:  random, random-generation
relude-random
Composable random generators based on the PCG paper
Stars: ✭ 15 (-98.63%)
Mutual labels:  random, random-generation
Mir Random
Advanced Random Number Generators
Stars: ✭ 30 (-97.26%)
Mutual labels:  random-generation, random
RandomGenKt
Kotlin port of RandomGen
Stars: ✭ 28 (-97.44%)
Mutual labels:  random, random-generation
jRand
A Java library to generate random data for all sorts of things. Java random data faker
Stars: ✭ 27 (-97.53%)
Mutual labels:  random, random-generation
strgen
A Python module for a template language that generates randomized data
Stars: ✭ 34 (-96.89%)
Mutual labels:  random, random-generation
Markovnamegenerator
✒️ Markov process-based procedural name and word generator
Stars: ✭ 300 (-72.6%)
Mutual labels:  random-generation, random
Minecraft Randomiser
Resource pack and data pack randomiser to shuffle textures, sounds, loot tables, recipes, and more
Stars: ✭ 34 (-96.89%)
Mutual labels:  random
Generate Random Web V7.19.03
Projeto para um cliente, afim de automatizar a escolha de números, pois o mesmo é jogador de loterias e queria um sistema para gerar jogos prontos.
Stars: ✭ 19 (-98.26%)
Mutual labels:  random-generation
Kolpa
A fake data generator written in and for Go
Stars: ✭ 645 (-41.1%)
Mutual labels:  random-generation
Chancejs
Chance - Random generator helper for JavaScript
Stars: ✭ 5,682 (+418.9%)
Mutual labels:  random
Kanye.rest
🌊 A free REST API for random Kanye West quotes (Kanye as a Service)
Stars: ✭ 558 (-49.04%)
Mutual labels:  random
Stuffz
Basically a script thrift shop
Stars: ✭ 539 (-50.78%)
Mutual labels:  random
Adventuresmith
Generate tabletop RPG nonsense at the push of a button! (on Android)
Stars: ✭ 54 (-95.07%)
Mutual labels:  random-generation
Troschuetz.random
Fully managed library providing various random number generators and distributions.
Stars: ✭ 47 (-95.71%)
Mutual labels:  random
Session Token
Secure, efficient, simple random session token generation
Stars: ✭ 12 (-98.9%)
Mutual labels:  random

Easy Random
The simple, stupid random Java™ beans generator

MIT license Build Status Maven Central Javadocs Project status


Project status

As of November 15, 2020, Easy Random is in maintenance mode. This means only bug fixes will be addressed from now on (except for records support which will be released when Java 16 is out). Version 5.0.x (based on Java 11) and version 4.3.x (based on Java 8) are the only supported versions for now. Please consider upgrading to one of these versions at your earliest convenience.

Latest news

  • 15/11/2020: Easy Random v5.0.0 is out and is now based on Java 11. Feature wise, this release is the same as v4.3.0. Please check the release notes for more details.
  • 07/11/2020: Easy Random v4.3.0 is now released with support for generic types and fluent setters! You can find all details in the change log.

What is Easy Random ?

Easy Random is a library that generates random Java beans. You can think of it as an ObjectMother for the JVM. Let's say you have a class Person and you want to generate a random instance of it, here we go:

EasyRandom easyRandom = new EasyRandom();
Person person = easyRandom.nextObject(Person.class);

The method EasyRandom#nextObject is able to generate random instances of any given type.

What is this EasyRandom API ?

The java.util.Random API provides 7 methods to generate random data: nextInt(), nextLong(), nextDouble(), nextFloat(), nextBytes(), nextBoolean() and nextGaussian(). What if you need to generate a random String? Or say a random instance of your domain object? Easy Random provides the EasyRandom API that extends java.util.Random with a method called nextObject(Class type). This method is able to generate a random instance of any arbitrary Java bean.

The EasyRandomParameters class is the main entry point to configure EasyRandom instances. It allows you to set all parameters to control how random data is generated:

EasyRandomParameters parameters = new EasyRandomParameters()
   .seed(123L)
   .objectPoolSize(100)
   .randomizationDepth(3)
   .charset(forName("UTF-8"))
   .timeRange(nine, five)
   .dateRange(today, tomorrow)
   .stringLengthRange(5, 50)
   .collectionSizeRange(1, 10)
   .scanClasspathForConcreteTypes(true)
   .overrideDefaultInitialization(false)
   .ignoreRandomizationErrors(true);

EasyRandom easyRandom = new EasyRandom(parameters);

For more details about these parameters, please refer to the configuration parameters section.

In most cases, default options are enough and you can use the default constructor of EasyRandom.

Easy Random allows you to control how to generate random data through the org.jeasy.random.api.Randomizer interface and makes it easy to exclude some fields from the object graph using a java.util.function.Predicate:

EasyRandomParameters parameters = new EasyRandomParameters()
   .randomize(String.class, () -> "foo")
   .excludeField(named("age").and(ofType(Integer.class)).and(inClass(Person.class)));

EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);

In the previous example, Easy Random will:

  • Set all fields of type String to foo (using the Randomizer defined as a lambda expression)
  • Exclude the field named age of type Integer in class Person.

The static methods named, ofType and inClass are defined in org.jeasy.random.FieldPredicates which provides common predicates you can use in combination to define exactly which fields to exclude. A similar class called TypePredicates can be used to define which types to exclude from the object graph. You can of course use your own java.util.function.Predicate in combination with those predefined predicates.

Why Easy Random ?

Populating a Java object with random data can look easy at first glance, unless your domain model involves many related classes. In the previous example, let's suppose the Person type is defined as follows:

Without Easy Random, you would write the following code in order to create an instance of the Person class:

Street street = new Street(12, (byte) 1, "Oxford street");
Address address = new Address(street, "123456", "London", "United Kingdom");
Person person = new Person("Foo", "Bar", "[email protected]", Gender.MALE, address);

And if these classes do not provide constructors with parameters (may be some legacy beans you can't change), you would write:

Street street = new Street();
street.setNumber(12);
street.setType((byte) 1);
street.setName("Oxford street");

Address address = new Address();
address.setStreet(street);
address.setZipCode("123456");
address.setCity("London");
address.setCountry("United Kingdom");

Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
person.setEmail("[email protected]");
person.setGender(Gender.MALE);
person.setAddress(address);

With Easy Random, generating a random Person object is done with new EasyRandom().nextObject(Person.class). The library will recursively populate all the object graph. That's a big difference!

How can this be useful ?

Sometimes, the test fixture does not really matter to the test logic. For example, if we want to test the result of a new sorting algorithm, we can generate random input data and assert the output is sorted, regardless of the data itself:

@org.junit.Test
public void testSortAlgorithm() {

   // Given
   int[] ints = easyRandom.nextObject(int[].class);

   // When
   int[] sortedInts = myAwesomeSortAlgo.sort(ints);

   // Then
   assertThat(sortedInts).isSorted(); // fake assertion

}

Another example is testing the persistence of a domain object, we can generate a random domain object, persist it and assert the database contains the same values:

@org.junit.Test
public void testPersistPerson() throws Exception {
   // Given
   Person person = easyRandom.nextObject(Person.class);

   // When
   personDao.persist(person);

   // Then
   assertThat("person_table").column("name").value().isEqualTo(person.getName()); // assretj db
}

There are many other uses cases where Easy Random can be useful, you can find a non exhaustive list in the wiki.

Extensions

Articles and blog posts

Who is using Easy Random ?

Contribution

You are welcome to contribute to the project with pull requests on GitHub. Please note that Easy Random is in maintenance mode, which means only pull requests for bug fixes will be considered.

If you believe you found a bug or have any question, please use the issue tracker.

Core team and contributors

Core team

Awesome contributors

Thank you all for your contributions!

License

The MIT License. See LICENSE.txt.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].