public class BeanTester extends Object
Concrete BeanTester that affords a means of testing JavaBean objects with respect to:
Each property is tested by:
Each property of a type is tested in turn. Each type is tested multiple times to reduce the risk of hard-coded values
within a getter or setter matching the random test values generated and the test failing to detect a bug.
Testing can be configured as follows:
See:
setIterations(int) to set the number of times any type is testedaddCustomConfiguration(Class<?>,Configuration) to add a custom Configuration across all testing of
the specified typetestBean(Class<?>,Configuration) to specify a custom Configuration for a single test scenariogetFactoryCollection().addFactory(Class<?>,Factory<?>) to add a custom Factory for a type across all
testingThe following example shows how to test a class MyClass:
BeanTester beanTester = new BeanTester(); beanTester.testBean(MyClass.class);
If the test fails, an AssertionError is thrown.
To ignore a property (say, lastName) when testing a class:
BeanTester beanTester = new BeanTester();
Configuration configuration = new ConfigurationBuilder().ignoreProperty("lastName").build();
beanTester.testBean(MyClass.class, configuration);
| Modifier and Type | Field and Description |
|---|---|
static int |
TEST_ITERATIONS_PER_BEAN
Default number of times a bean should be tested.
|
| Constructor and Description |
|---|
BeanTester()
|
| Modifier and Type | Method and Description |
|---|---|
FactoryCollection |
getFactoryCollection()
The collection of test data Factories with which you can register new Factories for custom Data Types.
|
int |
getIterations()
Get the number of times each bean should be tested.
|
RandomValueGenerator |
getRandomValueGenerator()
Get a RandomNumberGenerator.
|
protected void |
testBean(BeanInformation beanInformation,
Configuration configuration)
Test the type specified by the beanInformation parameter using the specified Configuration.
|
void |
testBean(Class<?> beanClass)
Test the type specified by the beanClass parameter.
|
void |
testBean(Class<?> beanClass,
Configuration customConfiguration)
Test the type specified by the beanClass parameter, using the custom Configuration provided as an override to any
global configuration settings.
|
void |
testBeans(Class<?>... beanClasses) |
public static final int TEST_ITERATIONS_PER_BEAN
public FactoryCollection getFactoryCollection()
public RandomValueGenerator getRandomValueGenerator()
public int getIterations()
Get the number of times each bean should be tested.
Note: A custom Configuration can override this global setting.
public void testBean(Class<?> beanClass) throws IllegalArgumentException, AssertionError, BeanTestException
Test the type specified by the beanClass parameter.
Testing will test each publicly readable and writable property of the specified beanClass to ensure that the getters and setters function correctly.
The test is performed repeatedly using random data for each scenario to prevent salient getter/setter failures.
When a test is failed, an AssertionError is thrown.
beanClass - The type to be tested.IllegalArgumentException - If the beanClass is deemed illegal. For example, if it is null.AssertionError - If the bean fails the test.BeanTestException - If an unexpected exception occurs during testing.public void testBeans(Class<?>... beanClasses) throws IllegalArgumentException, AssertionError, BeanTestException
public void testBean(Class<?> beanClass, Configuration customConfiguration) throws IllegalArgumentException, AssertionError, BeanTestException
Test the type specified by the beanClass parameter, using the custom Configuration provided as an override to any
global configuration settings.
Testing will test each publicly readable and writable property of the specified beanClass to ensure that the
getters and setters function correctly.
The test is performed repeatedly using random data for each scenario to prevent salient getter/setter failures.
When a test is failed, an AssertionError is thrown.
beanClass - The type to be tested.customConfiguration - The custom Configuration to be used when testing the specified beanClass type. This Configuration is
only used for this individual test and will not be retained for future testing of this or any other
type. To register a custom Configuration across multiple tests, use
addCustomConfiguration(). If no custom Configuration is required, pass null
or use testBean(Class<?>) instead.IllegalArgumentException - If the beanClass is deemed illegal. For example, if it is null.AssertionError - If the bean fails the test.BeanTestException - If an unexpected exception occurs during testing.protected void testBean(BeanInformation beanInformation, Configuration configuration) throws IllegalArgumentException, AssertionError, BeanTestException
Test the type specified by the beanInformation parameter using the specified Configuration.
Testing will test each publicly readable and writable property of the specified beanClass to ensure that the
getters and setters function correctly.
The test is performed repeatedly using random data for each scenario to prevent salient getter/setter failures.
When a test is failed, an AssertionError is thrown.
beanInformation - Information about the type to be tested.configuration - The custom Configuration to be used when testing the specified beanClass type. If no custom
Configuration is required, pass null or use testBean(Class<?>) instead.IllegalArgumentException - If the beanInformation is deemed illegal. For example, if it is null.AssertionError - If the bean fails the test.BeanTestException - If an unexpected exception occurs during testing.Copyright © 2010–2020 meanbean. All rights reserved.