Class AssertionArgs
This class encapsulates additional arguments and configuration options for assertion methods in the Bean-Centric Testing (BCT) framework. It provides a fluent API for customizing assertion behavior including custom converters and enhanced error messaging.
The primary purposes of this class are:
- Custom Bean Conversion: Override the default
BeanConverter
for specialized object introspection - Enhanced Error Messages: Add context-specific error messages with parameter substitution
- Fluent Configuration: Chain configuration calls for readable test setup
- Assertion Context: Provide additional context for complex assertion scenarios
Basic Usage:
Custom Bean Converter:
Advanced Error Messages:
Fluent Configuration:
Error Message Composition:
When assertion failures occur, error messages are intelligently composed:
- Base Message: Custom message set via
setMessage(String, Object...)
orsetMessage(Supplier)
- Assertion Context: Specific context provided by individual assertion methods
- Composite Format:
"{base message}, Caused by: {assertion context}"
Thread Safety:
This class is not thread-safe and is intended for single-threaded test execution.
Each test method should create its own instance using BctAssertions.args()
or create
a new instance directly with new AssertionArgs()
.
Immutability Considerations:
While this class uses fluent setters that return this
for chaining, the instance
is mutable. For reusable configurations across multiple tests, consider creating a factory
method that returns pre-configured instances.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Optional<BeanConverter>
Gets the configured bean converter, if any.Gets the base message supplier for composition with assertion-specific messages.getMessage
(String msg, Object... args) Composes the final error message by combining custom and assertion-specific messages.setBeanConverter
(BeanConverter value) Sets a customBeanConverter
for object introspection and property access.setMessage
(String message, Object... args) Sets a parameterized error message for assertion failures.setMessage
(Supplier<String> value) Sets a custom error message supplier for assertion failures.
-
Constructor Details
-
AssertionArgs
public AssertionArgs()Creates a new instance with default settings.Instances start with no custom bean converter and no custom error message. All assertion methods will use default behavior until configured otherwise.
-
-
Method Details
-
setBeanConverter
Sets a customBeanConverter
for object introspection and property access.The custom converter allows fine-tuned control over how objects are converted to strings, how collections are listified, and how nested properties are accessed. This is particularly useful for:
- Custom Object Types: Objects that don't follow standard JavaBean patterns
- Specialized Formatting: Custom string representations for assertion comparisons
- Performance Optimization: Cached or optimized property access strategies
- Domain-Specific Logic: Business-specific property resolution rules
Example:
// Create converter with custom stringifiers var converter = BasicBeanConverter.builder () .defaultSettings() .addStringifier(LocalDate.class ,date ->date .format(DateTimeFormatter.ISO_LOCAL_DATE )) .addStringifier(Money.class ,money ->money .getAmount().toPlainString()) .build();// Use in assertions assertBean (args ().setBeanConverter(converter ),order ,"date,total" ,"2023-12-01,99.99" );- Parameters:
value
- The custom bean converter to use. If null, assertions will fall back to the default converter.- Returns:
- This instance for method chaining.
-
getBeanConverter
Gets the configured bean converter, if any.- Returns:
- An Optional containing the custom converter, or empty if using default behavior.
-
setMessage
Sets a custom error message supplier for assertion failures.The supplier allows for dynamic message generation, including context that may only be available at the time of assertion failure. This is useful for:
- Timestamps: Including the exact time of failure
- Test State: Including runtime state information
- Expensive Operations: Deferring costly string operations until needed
- Conditional Messages: Different messages based on runtime conditions
Example:
// Dynamic message with timestamp assertBean (args ().setMessage(() ->"Test failed at " + Instant.now ()),result ,"status" ,"SUCCESS" );// Message with expensive computation assertBean (args ().setMessage(() ->"Failed after " + computeTestDuration() +" ms" ),response ,"error" ,"null" );- Parameters:
value
- The message supplier. Called only when an assertion fails.- Returns:
- This instance for method chaining.
-
setMessage
Sets a parameterized error message for assertion failures.This method uses
MessageFormat
to substitute parameters into the message template. The formatting occurs immediately when this method is called, not when the assertion fails.Parameter Substitution:
Uses standard MessageFormat patterns:
{0}
- First parameter{1}
- Second parameter{0,number,#}
- Formatted number{0,date,short}
- Formatted date
Examples:
// Simple parameter substitution assertBean (args ().setMessage("User {0} validation failed" ,userId ),user ,"active" ,"true" );// Multiple parameters assertBean (args ().setMessage("Test {0} failed on iteration {1}" ,testName ,iteration ),result ,"success" ,"true" );// Number formatting assertBean (args ().setMessage("Expected {0,number,#.##} but got different value" ,expectedValue ),actual ,"value" ,"123.45" );- Parameters:
message
- The message template with MessageFormat placeholders.args
- The parameters to substitute into the message template.- Returns:
- This instance for method chaining.
-
getMessage
Gets the base message supplier for composition with assertion-specific messages.- Returns:
- The configured message supplier, or null if no custom message was set.
-
getMessage
Composes the final error message by combining custom and assertion-specific messages.This method implements the message composition strategy used throughout the assertion framework:
- No Custom Message: Returns the assertion-specific message as-is
- With Custom Message: Returns
"{custom}, Caused by: {assertion}"
This allows tests to provide high-level context while preserving the specific technical details about what assertion failed.
- Parameters:
msg
- The assertion-specific message template.args
- Parameters for the assertion-specific message.- Returns:
- A supplier that produces the composed error message.
-