Code Monkey home page Code Monkey logo

java-random's Introduction

Advanced Random Test Data Utils

Project Documentation

System Status
Dependency Maven Central

This modules aims to provide a universal mechanism to create random test dummies of java objects.

Usage

Maven Dependency (Latest Version in pom.xml):

<dependency>
	<groupId>com.namics.oss</groupId>
	<artifactId>java-random</artifactId>
	<version>1.2.3</version>
</dependency>

Maven Central

Requirements

Minumum Java Version

  • Java: JDK 8

Java 9 Notes

  • Module is not prepared for java 9 yet.
  • Due to heavy use of reflection in this lib it might not be that simple to adopt java 9

Create random values

Basic values

The com.namics.commons.random.RandomData util provides a lot of convenience methods to create basic random values.

Java beans and advanced types

Use the generic method com.namics.commons.random.RandomData.random(Class<?> type) to create random instance of the object.

This works for types with available generators

Integer random = RandomData.random(Integer.class);

But also for Java beans:

 public class Demo {
 	public static class Person{
 		private String name;
 		public String getName() {return name;}
 		public void setName(String name) {this.name = name;}
 	}
 
 	@Test
 	public void simplePerson() throws Exception {
 		Person person = RandomData.random(Person.class);
 		System.out.println(person.getName()); // Leonel Bowers
 	}
 } 

This requires a registered instance of com.namics.commons.random.generator.RandomGenerator<SupportedType> for the requested type. There is a basic list of generators registered by default to support most basic type. See basic generators for complete list.

There is a basic support for Collections.

Register custom generators RandomGenerator<SupportedType>

There are several ways to register custom RandomGenerator<SupportedType>s:

  • auto discovery:
    • place RandomGenerator<SupportedType> in same package as type to be generated
    • add custom scan package RandomData.addRandomGenerators(String scanpackage )
  • explicit registration:
    • Register generator class RandomData.addRandomGenerator(Class<? extends RandomGenerator> generatorClass )
    • Register generator instance RandomData.addRandomGenerator(RandomGenerator generator)
    • Ein zu scannendes Package registrieren:

Accented chars in random names

You may enable international names by setting either

  • system property "random.names.international" to "true"
  • environment variable "RANDOM_NAMES_INTERNATIONAL" to "true"

if not enabled, only names without accents are used.

java-random's People

Contributors

namicsmachine avatar ernscht avatar zehndton 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.