Class BeanTester<T>
- Type Parameters:
T- The type of bean being tested
This class provides a fluent API for setting up and executing common bean tests including:
- Getter/setter validation using
BctAssertions.assertBean(Object, String, String) - Copy constructor/method testing
- JSON serialization/deserialization testing
- Round-trip serialization testing
- toString() method validation
Example Usage:
BeanTester<MyBean> tester = testBean(myBean) .json("{prop1:'value1',prop2:'value2'}") .props("prop1,prop2") .vals("'value1','value2'") .string("{prop1:'value1',prop2:'value2'}"); tester.assertGettersAndSetters(); tester.assertToJson(); tester.assertFromJson(); tester.assertRoundTrip(); tester.assertToString();
This class is typically used in nested test classes to provide consistent testing patterns across different bean types in the Apache Juneau project.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidValidates that the bean's copy constructor or copy() method works correctly.voidValidates that the expected JSON deserializes to a bean with correct property values.voidValidates that the bean's getters and setters work correctly.voidValidates that the bean can be serialized to JSON and deserialized back correctly.voidValidates that the bean serializes to the expected JSON format.voidValidates that the bean's toString() method returns the expected string.bean()Returns the bean instance being tested.Sets the bean instance to be tested.json()Returns the expected JSON representation of the bean.Sets the expected JSON representation of the bean.props()Returns the comma-delimited list of property names.Sets the comma-delimited list of property names for bean validation.string()Returns the expected string representation of the bean.Sets the expected string representation of the bean.vals()Returns the comma-delimited list of expected property values.Sets the comma-delimited list of expected property values for bean validation.
-
Constructor Details
-
BeanTester
public BeanTester()
-
-
Method Details
-
bean
Sets the bean instance to be tested.- Parameters:
value- The bean instance to test. Must not benull .- Returns:
- This object for method chaining.
-
bean
Returns the bean instance being tested.- Returns:
- The bean instance being tested.
-
json
Sets the expected JSON representation of the bean.This JSON string is used by
assertToJson()to verify that the bean serializes to the expected JSON format, and byassertFromJson()to verify that the JSON deserializes back to an equivalent bean.- Parameters:
value- The expected JSON representation.- Returns:
- This object for method chaining.
-
json
Returns the expected JSON representation of the bean.- Returns:
- The expected JSON representation.
-
props
Sets the comma-delimited list of property names for bean validation.This string is used by
BctAssertions.assertBean(Object, String, String)to validate that the bean has the expected properties with the expected values. Property names should be listed in the same order as their corresponding values in thevals(String)parameter.- Parameters:
value- Comma-delimited list of property names (e.g., "prop1,prop2,prop3").- Returns:
- This object for method chaining.
-
props
Returns the comma-delimited list of property names.- Returns:
- The comma-delimited list of property names.
-
vals
Sets the comma-delimited list of expected property values for bean validation.This string is used by
BctAssertions.assertBean(Object, String, String)to validate that the bean properties have the expected values. Values should be listed in the same order as their corresponding property names in theprops(String)parameter. String values should be quoted (e.g., "'value'").- Parameters:
value- Comma-delimited list of expected values (e.g., "'value1','value2',123").- Returns:
- This object for method chaining.
-
vals
Returns the comma-delimited list of expected property values.- Returns:
- The comma-delimited list of expected property values.
-
string
Sets the expected string representation of the bean.This string is used by
assertToString()to verify that the bean'stoString()method returns the expected value.- Parameters:
value- The expected string representation.- Returns:
- This object for method chaining.
-
string
Returns the expected string representation of the bean.- Returns:
- The expected string representation.
-
assertGettersAndSetters
Validates that the bean's getters and setters work correctly.This method uses
BctAssertions.assertBean(Object, String, String)to verify that the bean's properties match the expected values specified byprops(String)andvals(String).- Throws:
AssertionError- if any property values don't match expectations.
-
assertCopy
Validates that the bean's copy constructor or copy() method works correctly.This method uses reflection to invoke the bean's
copy()method, then verifies:- The returned object is not the same instance as the original
- The returned object has the same property values as the original
- Throws:
AssertionError- if the copy is the same instance or has different property values.RuntimeException- if the bean doesn't have acopy()method or it fails to invoke.
-
assertToJson
Validates that the bean serializes to the expected JSON format.This method verifies that the bean serializes (via JSON5) to the JSON string specified by
json(String).- Throws:
AssertionError- if the serialized JSON doesn't match the expected format.
-
assertFromJson
Validates that the expected JSON deserializes to a bean with correct property values.This method deserializes the JSON string specified by
json(String)and verifies that the resulting bean has the expected property values specified byprops(String)andvals(String).- Throws:
AssertionError- if the deserialized bean doesn't have the expected property values.RuntimeException- if JSON deserialization fails.
-
assertRoundTrip
Validates that the bean can be serialized to JSON and deserialized back correctly.This method performs a complete round-trip test by:
- Serializing the bean to JSON
- Deserializing the JSON back to a bean
- Verifying the resulting bean has the expected property values
- Throws:
AssertionError- if the round-trip bean doesn't have the expected property values.RuntimeException- if serialization or deserialization fails.
-
assertToString
Validates that the bean's toString() method returns the expected string.This method compares the bean's
toString()output with the string specified bystring(String).- Throws:
AssertionError- if the toString() output doesn't match the expected string.
-