Class BeanTester<T>

java.lang.Object
org.apache.juneau.BeanTester<T>
Type Parameters:
T - The type of bean being tested

public class BeanTester<T> extends Object
Utility class for testing bean functionality in a standardized way.

This class provides a fluent API for setting up and executing common bean tests including:

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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Validates that the bean's copy constructor or copy() method works correctly.
    void
    Validates that the expected JSON deserializes to a bean with correct property values.
    void
    Validates that the bean's getters and setters work correctly.
    void
    Validates that the bean can be serialized to JSON and deserialized back correctly.
    void
    Validates that the bean serializes to the expected JSON format.
    void
    Validates that the bean's toString() method returns the expected string.
    Returns the bean instance being tested.
    bean(T value)
    Sets the bean instance to be tested.
    Returns the expected JSON representation of the bean.
    json(String value)
    Sets the expected JSON representation of the bean.
    Returns the comma-delimited list of property names.
    props(String value)
    Sets the comma-delimited list of property names for bean validation.
    Returns the expected string representation of the bean.
    string(String value)
    Sets the expected string representation of the bean.
    Returns the comma-delimited list of expected property values.
    vals(String value)
    Sets the comma-delimited list of expected property values for bean validation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • bean

      public BeanTester<T> bean(T value)
      Sets the bean instance to be tested.
      Parameters:
      value - The bean instance to test. Must not be null.
      Returns:
      This object for method chaining.
    • bean

      public T bean()
      Returns the bean instance being tested.
      Returns:
      The bean instance being tested.
    • json

      public BeanTester<T> json(String value)
      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 by assertFromJson() to verify that the JSON deserializes back to an equivalent bean.

      Parameters:
      value - The expected JSON representation.
      Returns:
      This object for method chaining.
    • json

      public String json()
      Returns the expected JSON representation of the bean.
      Returns:
      The expected JSON representation.
    • props

      public BeanTester<T> props(String value)
      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 the vals(String) parameter.

      Parameters:
      value - Comma-delimited list of property names (e.g., "prop1,prop2,prop3").
      Returns:
      This object for method chaining.
    • props

      public String props()
      Returns the comma-delimited list of property names.
      Returns:
      The comma-delimited list of property names.
    • vals

      public BeanTester<T> vals(String value)
      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 the props(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

      public String vals()
      Returns the comma-delimited list of expected property values.
      Returns:
      The comma-delimited list of expected property values.
    • string

      public BeanTester<T> string(String value)
      Sets the expected string representation of the bean.

      This string is used by assertToString() to verify that the bean's toString() method returns the expected value.

      Parameters:
      value - The expected string representation.
      Returns:
      This object for method chaining.
    • string

      public String string()
      Returns the expected string representation of the bean.
      Returns:
      The expected string representation.
    • assertGettersAndSetters

      public void 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 by props(String) and vals(String).

      Throws:
      AssertionError - if any property values don't match expectations.
    • assertCopy

      public void 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 a copy() method or it fails to invoke.
    • assertToJson

      public void 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

      public void 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 by props(String) and vals(String).

      Throws:
      AssertionError - if the deserialized bean doesn't have the expected property values.
      RuntimeException - if JSON deserialization fails.
    • assertRoundTrip

      public void assertRoundTrip()
      Validates that the bean can be serialized to JSON and deserialized back correctly.

      This method performs a complete round-trip test by:

      1. Serializing the bean to JSON
      2. Deserializing the JSON back to a bean
      3. 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

      public void assertToString()
      Validates that the bean's toString() method returns the expected string.

      This method compares the bean's toString() output with the string specified by string(String).

      Throws:
      AssertionError - if the toString() output doesn't match the expected string.