-
Notifications
You must be signed in to change notification settings - Fork 3
Home
Welcome to the Mean Bean User Guide. Mean Bean is an Open Source Java test library whose purpose is to make testing JavaBeans and “plain old java objects” (POJOs) as easy as possible.
At present Mean Bean provides these main features:
- It tests that the getter and setter method pairs of a class function correctly.
- It verifies that the equals and hashCode methods of a class comply with the Equals Contract and HashCode Contract respectively.
- It verifies that the properties you believe are considered in an equals method are actually considered, and vice versa. We call this "property significance verification"
- It can test that a custom toString() is defined.
Mean Bean is not tied into any specific testing or unit testing framework. When Mean Bean detects a test failure, it ultimately just throws a standard java.lang.AssertionError with an appropriate message.
To use Mean Bean in a JUnit test, simply call it. For example, this JUnit example test uses a BeanVerification to test:
- the public getter and setter methods of the class
- equals, hashCode and toString methods
@Test
public void testBeean() {
BeanVerification.verifyBean(MyJavaBeanDomainObject.class);
}
You can easily test the getter and setter methods of a class using Mean Bean's BeanVerification or its underlying BeanTester. Pass the class in question to the verifyBean() method and Mean Bean will throw an AssertionError if any getter or setter methods behave unexpectedly. Currently Mean Bean only tests properties that have both a public getter and setter method. We will extend test coverage to include reduced levels of visibility in the future.
The BeanVerification.verifyBean method implements the following algorithm:
for i in 1 .. n:
for each property in public getter/setter method pairs:
generate suitable test data for property
invoke setter with test data
invoke getter
test that getter returned same value as passed to setter
Where n is the number of test iterations to perform per class. The default is 100, but this can be altered. Test configuration is covered later in this guide.If the getter does not return the same value as the test data passed to the setter, the test has failed and an AssertionError is thrown with an appropriate message.
Mean Bean ships with Factories that generate test data for all standard Java types.
- primitive types: boolean, byte, short, int, long, float, double, char
- basic object types: Boolean, Byte, Short, Integer, Long, Float, Double, BigDecimal, BigInteger, Character, String, Date, UUID
- java.time types: Clock, Instant, LocalDate, LocalDateTime, OffsetDateTime, OffsetTime, MonthDay, Year, YearMonth, ZonedDateTime, ZoneId, ZoneOffset, Duration, Peroid
- java.net types: URL, URI
- io types: File, Path
- concurrent types: AtomicInteger, AtomicLong, AtomicBoolean
- misc types: Locale
- array types: of any dimension and component type
- Lists, Sets, Maps
If you think we've missed a "standard" type, please let us know and we'll add it. We'll cover how to create and register your own Factories later in this guide.
When Mean Bean encounters a property type it does not recognize, it cannot use any of the Factories it ships with. For example, when testing the Person class, the Address would not be recognized:
class Person {
private Address address;
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
}
In this situation, Mean Bean will try to create a special Factory on-the-fly based on the unrecognized type. This special Factory will create a new instance of the type (e.g. Address), set all of its public properties, and use it as test data. This will only be possible if the type has a no-arg constructor. If Mean Bean succeeds in instantiating an instance of the unrecognized type, it logs a warning. If you find that Mean Bean is failing to recognize what you believe is a standard type, please let us know and we will add it. If the unrecognized type does not have a no-arg constructor, an exception will be thrown instructing you to register a custom Factory. Creation and registration of custom Factories is covered later in this guide.
The following example uses the BeanTester default behavior. It tests that the public getter/setter method pairs of the class MyJavaBeanDomainObject function correctly. The default behavior of the BeanVerification is to test each and every public getter and setter method pair with randomly generated values n times. The default value of n is 100.
BeanVerification.verifyBean(MyJavaBeanDomainObject.class);
The number of times each getter/setter pair is tested can be altered for all classes by setting the iterations on the BeanTesterBuilder instance before invoking the testBean method on any class you want to test. The following example changes the number of iterations to 75, meaning that each getter/setter pair of MyJavaBeanDomainObject and AnotherJavaBeanDomainObject is tested 75 times:
BeanVerification.verifyThat(EmployeeId.class)
.with(customizer -> customizer.setDefaultIterations(75))
.isValidJavaBean();
You can instruct Mean Bean to ignore (do not test) a named getter/setter pair ("property") like this:
BeanVerification.verifyThat(EmployeeId.class)
.with(customizer -> customizer.addIgnoredProperty(EmployeeId::getId))
.isValidJavaBean();
Old Mean Bean 2.0 User Guide may be helpful.
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.