A JUnit 5 extension that automatically generates random test data for your test method parameters. Stop writing boilerplate test fixtures — just annotate your parameters with @Randomize and focus on what matters.
@ExtendWith(RandomParametersExtension.class)
class MyTest {
@Test
void test_with_random_data(@Randomize int value, @Randomize String name) {
// value and name are randomly generated each run
}
}<dependency>
<groupId>io.github.lyang</groupId>
<artifactId>random-params-resolver</artifactId>
<version>VERSION</version>
<scope>test</scope>
</dependency>testImplementation 'io.github.lyang:random-params-resolver:VERSION'| Type | Annotation Options | Default Range |
|---|---|---|
int / Integer |
intMin, intMax |
[Integer.MIN_VALUE, Integer.MAX_VALUE) |
long / Long |
longMin, longMax |
[Long.MIN_VALUE, Long.MAX_VALUE) |
float / Float |
floatMin, floatMax |
[0, 1) |
double / Double |
doubleMin, doubleMax |
[0, 1) |
BigInteger |
longMin, longMax |
[Long.MIN_VALUE, Long.MAX_VALUE) |
BigDecimal |
doubleMin, doubleMax |
[0, 1) |
byte[] |
length |
5 bytes |
String |
length, unicodeBlocks |
5 chars, BASIC_LATIN |
RandomGenerator |
seed |
seeded from System.nanoTime() |
@ExtendWith(RandomParametersExtension.class)
class NumericTest {
@Test
void random_integers(@Randomize int a, @Randomize Integer b) {
// each parameter gets a different random value
}
@Test
void bounded_range(@Randomize(intMin = 1, intMax = 100) int value) {
// value is in [1, 100)
}
@Test
void floating_point(
@Randomize(floatMin = 0.0f, floatMax = 1.0f) float ratio,
@Randomize(doubleMin = -180.0, doubleMax = 180.0) double longitude) {
}
@Test
void big_numbers(@Randomize BigInteger bigInt, @Randomize BigDecimal bigDec) {
}
}@ExtendWith(RandomParametersExtension.class)
class StringTest {
@Test
void random_string(@Randomize String value) {
// 5 random BASIC_LATIN characters
}
@Test
void custom_string(@Randomize(length = 20, unicodeBlocks = "EMOTICONS") String emojis) {
// 20 random emoji characters
}
@Test
void random_bytes(@Randomize(length = 16) byte[] token) {
// 16 random bytes
}
}Every test run logs the seed used for each parameter:
INFO: Using seed 787681803879958 for MyTest#my_test#arg0
To reproduce a failure, pin the seed:
@Test
void reproduce_failure(@Randomize(seed = 787681803879958L) int value) {
// always generates the same value
}@Test
void custom_generation(@Randomize RandomGenerator random) {
// use the generator directly for complex test data setup
var ids = random.ints(10, 1, 1000).boxed().toList();
}