Class BctConfiguration
This class provides thread-local configuration settings for BCT assertions, allowing test-specific customization of behavior such as map sorting, collection sorting, and bean converter selection.
Configuration is managed using thread-local storage, ensuring that parallel test execution doesn't
interfere with each other's settings. Settings are typically configured via the @BctConfig
annotation, but can also be set programmatically.
Configuration Properties:
BCT_SORT_MAPS: Enable sorting of maps in assertionsBCT_SORT_COLLECTIONS: Enable sorting of collections in assertions- Bean Converter: Custom bean converter instance for property access and conversion
Usage via Annotation:
Usage via Programmatic API:
Thread Safety:
All methods in this class are thread-safe. Configuration is stored in thread-local storage, ensuring that each test thread has its own isolated configuration that doesn't interfere with other concurrently running tests.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clears all thread-local configuration settings.static voidClears all global configuration settings.static StringSettingGets a configuration setting by name.static <T> TGets a configuration value by name with a default value.static voidSets a thread-local configuration value.static voidset(BeanConverter converter) Sets a custom bean converter for the current thread.static voidSets a global configuration value.
-
Field Details
-
BCT_SORT_MAPS
Configuration property name for enabling map sorting in BCT assertions.When set to
true , maps will be sorted by key before comparison in assertions. This ensures consistent ordering regardless of the map's internal ordering.Example:
BctConfiguration.
set (BctConfiguration.BCT_SORT_MAPS ,true );- See Also:
-
BCT_SORT_COLLECTIONS
Configuration property name for enabling collection sorting in BCT assertions.When set to
true , collections will be sorted before comparison in assertions. This ensures consistent ordering regardless of the collection's internal ordering.Example:
BctConfiguration.
set (BctConfiguration.BCT_SORT_COLLECTIONS ,true );- See Also:
-
-
Constructor Details
-
BctConfiguration
public BctConfiguration()
-
-
Method Details
-
set
Sets a thread-local configuration value.The value is stored in thread-local storage and will only affect the current thread. This is the recommended way to set configuration for individual tests.
Example:
BctConfiguration.
set (BctConfiguration.BCT_SORT_MAPS ,true );- Parameters:
name- The configuration property name (e.g.,BCT_SORT_MAPS).value- The value to set.- See Also:
-
setGlobal
Sets a global configuration value.Global values apply to all threads and persist until explicitly cleared. Use with caution in multi-threaded test environments.
Example:
BctConfiguration.
setGlobal (BctConfiguration.BCT_SORT_MAPS ,true );- Parameters:
name- The configuration property name (e.g.,BCT_SORT_MAPS).value- The value to set.- See Also:
-
get
Gets a configuration setting by name.Returns the setting object which can be used to check if a value is set and retrieve it. Thread-local values take precedence over global values.
- Parameters:
name- The configuration property name (e.g.,BCT_SORT_MAPS).- Returns:
- The setting object, or
null if not set. - See Also:
-
get
Gets a configuration value by name with a default value.Returns the configured value if set, otherwise returns the provided default value. Thread-local values take precedence over global values.
Example:
boolean sortMaps = BctConfiguration.get (BctConfiguration.BCT_SORT_MAPS ,false );- Type Parameters:
T- The type of the value.- Parameters:
name- The configuration property name (e.g.,BCT_SORT_MAPS).def- The default value to return if not set.- Returns:
- The configured value, or the default if not set.
- See Also:
-
clear
Clears all thread-local configuration settings.This method removes all thread-local settings including:
- Configuration properties (e.g.,
BCT_SORT_MAPS,BCT_SORT_COLLECTIONS) - Bean converter override
This is typically called in test teardown methods (e.g.,
@AfterEach) to ensure a clean state for subsequent tests. TheBctConfigExtensionautomatically calls this method after each test.Example:
@AfterEach void tearDown() { BctConfiguration.clear (); }- See Also:
- Configuration properties (e.g.,
-
clearGlobal
Clears all global configuration settings.This method removes all global settings. Use with caution as this affects all threads.
- See Also:
-
set
Sets a custom bean converter for the current thread.This method allows you to override the default converter (
BasicBeanConverter.DEFAULT) for all assertions in the current test method. The converter will be used by all assertion methods in the current thread.This is particularly useful when you need custom property access, stringification, or conversion logic for specific tests. The converter can be configured via the
@BctConfigannotation or set programmatically.Usage Example:
@BeforeEach void setUp() {var customConverter = BasicBeanConverter.builder () .defaultSettings() .addStringifier(LocalDate.class ,date ->date .format(DateTimeFormatter.ISO_LOCAL_DATE )) .build(); BctConfiguration.set (customConverter ); }@Test void testWithCustomConverter() {// All assertions use the custom converter assertBean (event ,"date" ,"2023-12-01" ); }@AfterEach void tearDown() { BctConfiguration.clear ();// Also clears converter override }Thread Safety:
This method is thread-safe and uses thread-local storage. Each test method running in parallel will have its own converter instance, preventing cross-thread interference.
- Parameters:
converter- The bean converter to use for the current thread. Must not benull .- Throws:
IllegalArgumentException- If converter isnull .- See Also:
-