Skip to content

Latest commit

 

History

History
157 lines (115 loc) · 7.18 KB

README.md

File metadata and controls

157 lines (115 loc) · 7.18 KB

What is Random Beans ?

Random Beans is a library that generates random Java beans. Let's say you have a class Person and you want to generate a random instance of it, here we go:

Person person = random(Person.class);

The static method EnhancedRandom.random is able to generate random instances of a given type.

Let's see another example. If you want to generate a random stream of 10 persons, you can use the following snippet:

Stream<Person> persons = randomStreamOf(10, Person.class);

This second static method of the EnhancedRandom API generates a stream of random instances of a given type.

What is this EnhancedRandom 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? Random Beans provides the EnhancedRandom 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:

EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder().build();
Person person = enhancedRandom.nextObject(Person.class);

The EnhancedRandomBuilder is the main entry point to configure EnhancedRandom instances with a fluent API. It allows you to set all parameters to control how random data is generated:

EnhancedRandom random = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
   .maxCollectionSize(10)
   .maxStringLength(20)
   .charset(forName("UTF-8"))
   .dateRange(today, tomorrow)
   .timeRange(nine, five)
   .scanClasspathForConcreteTypes(true)
   .seed(123L)
   .build();

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

In most cases, default options are enough and you can use a static import of the EnhancedRandom.random(Class object) as seen previously.

Why Random Beans ?

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 Random Beans, 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 don't have the control over), 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 Random Beans, generating a random Person object is done with random(Person.class). The library will recursively populate all the object graph. That's a big difference!

Quick links

Item Link
Documentation https://github.com/benas/random-beans/wiki
Continuous integration Build job @ Travis CI
Agile Board Backlog items @ waffle.io
Code coverage Coverage Status
Dependencies Dependency Status

Current version

  • The current stable version is 3.1.0 : Maven Central
  • The current development version is 3.2.0-SNAPSHOT : Build Status

You can find more details on how to get started here.

Contribution

You are welcome to contribute to the project with pull requests on GitHub.

If you believe you found a bug, please use the issue tracker.

If you have any question, suggestion, or feedback, do not hesitate to use the Gitter channel of the project.

Awesome contributors

Thank you all for your contributions!

License

Random Beans is released under the MIT license.

The MIT License

Copyright (c) 2016, Mahmoud Ben Hassine ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.