public class RandomBeansExtension extends Object implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.ParameterResolver
Usage examples:
Injecting random values as fields:
@ExtendWith(RandomBeansExtension.class)
public class MyTest {
@Random
private String anyString;
@Random(excluded = {"name", "value"})
private List anyPartiallyPopulatedDomainObject;
@Random(type = DomainObject.class)
private List anyDomainObjects;
@Test
public void testUsingRandomString() {
// use the injected anyString
// ...
}
@Test
public void testUsingRandomDomainObjects() {
// use the injected anyDomainObjects
// the anyDomainObjects will contain _N_ fully populated random instances of DomainObject
// ...
}
@Test
public void testUsingPartiallyPopulatedDomainObject() {
// use the injected anyPartiallyPopulatedDomainObject
// this object's "name" and "value" members will not be populated since this has been declared with
// excluded = {"name", "value"}
// ...
}
}
Injecting random values as parameters:
@ExtendWith(RandomBeansExtension.class)
public class MyTest {
@Test
@ExtendWith(RandomBeansExtension.class)
public void testUsingRandomString(@Random String anyString) {
// use the provided anyString
// ...
}
@Test
@ExtendWith(RandomBeansExtension.class)
public void testUsingRandomDomainObjects(@Random(type = DomainObject.class) List anyDomainObjects) {
// use the injected anyDomainObjects
// the anyDomainObjects will contain _N_ fully populated random instances of DomainObject
// ...
}
@Test
@ExtendWith(RandomBeansExtension.class)
public void testUsingPartiallyPopulatedDomainObject(@Random(excluded = {"name", "value"}) List anyPartiallyPopulatedDomainObject) {
// use the injected anyPartiallyPopulatedDomainObject
// this object's "name" and "value" members will not be populated since this has been declared with
// excluded = {"name", "value"}
// ...
}
}
| Constructor and Description |
|---|
RandomBeansExtension()
Create the extension with a default
EnhancedRandom. |
RandomBeansExtension(io.github.benas.randombeans.api.EnhancedRandom enhancedRandom)
Create the extension with the given
EnhancedRandom. |
| Modifier and Type | Method and Description |
|---|---|
void |
postProcessTestInstance(Object testInstance,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Inject random values into any fields which are annotated with
Random. |
Object |
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Provides a value for any parameter context which has passed the
supportsParameter(ParameterContext, ExtensionContext) gate. |
boolean |
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Does this extension support injection for parameters of the type described by the given
parameterContext? |
public RandomBeansExtension()
EnhancedRandom.public RandomBeansExtension(io.github.benas.randombeans.api.EnhancedRandom enhancedRandom)
EnhancedRandom. This is used, instead of the zero-arg alternative, when
the caller wants to override the default 'randomizer' configuration. This constructor will be called by using the
RegisterExtension annotation.enhancedRandom - public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws org.junit.jupiter.api.extension.ParameterResolutionException
parameterContext?supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolverparameterContext - the context for the parameter for which an argument should be resolvedextensionContext - the extension context for the Executable about to be invokedparameterContext is annotated with Random, false
otherwiseorg.junit.jupiter.api.extension.ParameterResolutionExceptionpublic Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
supportsParameter(ParameterContext, ExtensionContext) gate.resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolverparameterContext - the context for the parameter for which an argument should be resolvedextensionContext - the context in which the current test or container is being
executedorg.junit.jupiter.api.extension.ParameterResolutionExceptionpublic void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
Random.
This method doesn't populate static fields if they have values.postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessortestInstance - the instance to post-processextensionContext - the current extension contextExceptionCopyright © 2022. All rights reserved.