org.meanbean.test.BeanVerifier Maven / Gradle / Ivy
/*-
*
* meanbean
*
* Copyright (C) 2010 - 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.meanbean.test;
import org.meanbean.util.ClassPathUtils;
import java.util.function.Consumer;
/**
* BeanVerifier can be used in unit tests to verify
*
* - bean getter/setter methods
* - equals/hashCode methods
* - toString method
*
*
* Example usage:
*
* BeanVerifier.verifyBean(Company.class); // verify bean methods, equals/hashCode and toString
*
* BeanVerifier.verifyBeans(Company.class, Employee.class);
*
* BeanVerifier.forClass(Company.class)
* .withSettings(settings -> settings.setDefaultIterations(12))
* .withSettings(settings -> settings.addIgnoredProperty(Company::getName)) // exclude name property in bean getter/setter test
* .verifyGettersAndSetters()
*
*/
public interface BeanVerifier {
/**
* Start a fluent bean verification chain for the given beanClass
*/
public static BeanVerifier forClass(Class> beanClass) {
return new BeanVerifierImpl(beanClass);
}
/**
* Verify that given beanClass has valid bean getters/setters, equals/hashCode, and toString methods
*/
public static void verifyBean(Class> beanClass) {
forClass(beanClass).verify();
}
/**
* Verify that given beanClasses have valid bean getters/setters, equals/hashCode, and toString methods
*/
public static void verifyBeans(Class>... beanClasses) {
for (Class> beanClass : beanClasses) {
try {
BeanVerifier.verifyBean(beanClass);
} catch (AssertionError | RuntimeException e) {
throw new AssertionError("Cannot verify bean type " + beanClass.getName(), e);
}
}
}
/**
* Verify that bean classes in given packageName have valid bean getters/setters, equals/hashCode, and toString methods
*/
public static void verifyBeansIn(String packageName) {
Class>[] beanClasses = ClassPathUtils.findClassesIn(packageName);
verifyBeans(beanClasses);
}
/**
* Verify that bean classes in given packageObj have valid bean getters/setters, equals/hashCode, and toString methods
*/
public static void verifyBeansIn(Package packageObj) {
verifyBeansIn(packageObj.getName());
}
/**
* Customizes bean verification settings. Example:
*
* BeanVerifier.forClass(Company.class)
* .withSettings(settings -> settings.registerFactory(Employee.class, () -> createEmployee())
* .withSettings(settings -> settings.setDefaultIterations(10))
*
*/
BeanVerifier withSettings(Consumer verifierSettingsEditor);
/**
* Customizes bean verification settings. Example:
*
* BeanVerifier.forClass(Company.class)
* .editSettings() // start editing settings
* .setDefaultIterations(12)
* .addEqualsInsignificantProperty(Company::getId)
* .registerFactory(Company.class, () -> company)
* .addIgnoredProperty(Company::getName)
* .edited() // finish editing settings and perform verifications
* .verifyGettersAndSetters();
*
*/
VerifierSettingsEditor editSettings();
/**
* Checks the bean's equals and hashCode methods.
*
* @see EqualsMethodTester
* @see HashCodeMethodTester
*/
BeanVerifier verifyEqualsAndHashCode();
/**
* Checks the bean's public getter and setter methods.
*
* Each property is tested by:
*
*
* - generating a random test value for the specific property type
* - invoking the property setter method, passing the generated test value
* - invoking the property getter method and obtaining the return value
* - verifying that the value obtained from the getter method matches the value passed to the setter method
*
*
*
* 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.
*
*
* @see BeanTester
*/
BeanVerifier verifyGettersAndSetters();
/**
* Checks that the bean overrides the default to Object::toString method.
*
* @see ToStringMethodTester
*/
BeanVerifier verifyToString();
/**
* Performs {@link #verifyGettersAndSetters()}, {@link #verifyEqualsAndHashCode()} and {@link #verifyToString()}
*/
default void verify() {
verifyGettersAndSetters()
.verifyEqualsAndHashCode()
.verifyToString();
}
}