Package org.apache.juneau.junit.bct


package org.apache.juneau.junit.bct
Bean-Centric Testing Framework.

This package provides a comprehensive testing framework that extends JUnit with streamlined assertion methods for Java objects. The Bean-Centric Testing (BCT) framework eliminates verbose test code while providing comprehensive object introspection and comparison capabilities.

Key Features:
  • Concise Assertions: Replace multiple lines of manual property extraction with single assertion calls
  • Powerful Property Access: Nested objects, collections, arrays, and maps with unified syntax
  • Flexible Comparison: Support for custom converters, formatters, and comparison logic
  • Type Safety: Comprehensive error messages with clear property paths
  • Extensible: Custom property extractors, stringifiers, and conversion logic
Core Classes:
Quick Start:

import static com.sfdc.junit.bct.BctAssertions.*; @Test void testUser() { User user = new User("Alice", 25, true); // Test multiple properties at once assertBean(user, "name,age,active", "Alice,25,true"); // Test nested objects assertBean(user, "address{street,city}", "{123 Main St,Springfield}"); }

Assertion Method Examples:
1. assertBean()

Tests object properties with support for nested syntax and collection iteration.

User user = new User("Bob", 30); user.setAddress(new Address("456 Oak Ave", "Denver")); // Test simple properties assertBean(user, "name", "Bob"); assertBean(user, "name,age", "Bob,30"); // Test nested properties assertBean(user, "address.street", "456 Oak Ave"); assertBean(user, "address{street,city}", "{456 Oak Ave,Denver}");

2. assertBeans()

Tests collections of objects by extracting and comparing specific fields.

List<User> users = Arrays.asList( new User("Alice", 25), new User("Bob", 30), new User("Carol", 35) ); // Test single field across collection assertBeans(users, "name", "Alice", "Bob", "Carol"); // Test multiple fields assertBeans(users, "name,age", "Alice,25", "Bob,30", "Carol,35");

3. assertMapped()

Tests custom property access using BiFunction for non-standard objects.

Map<String,Object> data = new HashMap<>(); data.put("name", "Alice"); data.put("score", 95); // Custom property extractor for Map objects assertMapped(data, (obj, key) -> obj.get(key), "name,score", "Alice,95");

4. assertList()

Tests list/collection elements with varargs for expected values.

List<String> names = Arrays.asList("Alice", "Bob", "Carol"); String[] colors = {"red", "green", "blue"}; // Test list contents assertList(names, "Alice", "Bob", "Carol"); assertList(colors, "red", "green", "blue");

5. assertContains()

Tests that a string appears somewhere within the stringified object.

User user = new User("Alice Smith", 25); List<String> items = Arrays.asList("apple", "banana", "cherry"); // Test substring presence assertContains("Alice", user); assertContains("banana", items);

6. assertContainsAll()

Tests that all specified strings appear within the stringified object.

User user = new User("Alice Smith", 25); user.setEmail("alice@example.com"); // Test multiple substrings assertContainsAll(user, "Alice", "Smith", "25"); assertContainsAll(user, "alice", "example.com");

7. assertEmpty()

Tests that collections, arrays, maps, or strings are empty.

List<String> emptyList = new ArrayList<>(); String[] emptyArray = {}; Map<String,String> emptyMap = new HashMap<>(); String emptyString = ""; // Test empty collections assertEmpty(emptyList); assertEmpty(emptyArray); assertEmpty(emptyMap); assertEmpty(emptyString);

8. assertNotEmpty()

Tests that collections, arrays, maps, or strings are not empty.

List<String> names = Arrays.asList("Alice"); String[] colors = {"red"}; Map<String,String> config = Map.of("key", "value"); String message = "Hello"; // Test non-empty collections assertNotEmpty(names); assertNotEmpty(colors); assertNotEmpty(config); assertNotEmpty(message);

9. assertSize()

Tests the size/length of collections, arrays, maps, or strings.

List<String> names = Arrays.asList("Alice", "Bob", "Carol"); String[] colors = {"red", "green"}; Map<String,Integer> scores = Map.of("Alice", 95, "Bob", 87); String message = "Hello"; // Test collection sizes assertSize(3, names); assertSize(2, colors); assertSize(2, scores); assertSize(5, message);

10. assertString()

Tests the string representation of an object using the configured converter.

User user = new User("Alice", 25); List<Integer> numbers = Arrays.asList(1, 2, 3); Date date = new Date(1609459200000L); // 2021-01-01 // Test string representations assertString("User(name=Alice, age=25)", user); assertString("[1, 2, 3]", numbers); assertString("2021-01-01", date);

11. assertMatchesGlob()

Tests that the stringified object matches a glob-style pattern (* and ? wildcards).

User user = new User("Alice Smith", 25); String filename = "report.pdf"; String email = "alice@company.com"; // Test pattern matching assertMatchesGlob("*Alice*", user); assertMatchesGlob("*.pdf", filename); assertMatchesGlob("*@*.com", email); assertMatchesGlob("User(name=Alice*, age=25)", user);

Custom Configuration with AssertionArgs:

All assertion methods support custom configuration through AssertionArgs:

// Custom error message assertBean(args("User validation failed"), user, "name,age", "Alice,25"); // Custom converter configuration AssertionArgs args = args() .setConverter(BasicBeanConverter.builder().defaultSettings() .setSetting("nullValue", "<empty>") .build()); assertBean(args, user, "name,nickname", "Alice,<empty>");

See Also:
  • Class
    Description
    Configuration and context object for advanced assertion operations.
    Default implementation of BeanConverter for Bean-Centric Test (BCT) object conversion.
    Builder for creating customized BasicBeanConverter instances.
    Comprehensive utility class for Bean-Centric Tests (BCT) and general testing operations.
    Abstract interface for Bean-Centric Test (BCT) object conversion and property access.
    Functional interface for converting collection-like objects to standardized List<Object> format.
    Collection of standard listifier implementations for the Bean-Centric Testing framework.
    Interface for custom property extraction strategies in the Bean-Centric Testing framework.
    Collection of standard property extractor implementations for the Bean-Centric Testing framework.
    Property extractor for array and collection objects with numeric indexing and size access.
    Property extractor for Map objects with direct key access and size property.
    Standard JavaBean property extractor using reflection.
    Exception thrown when a requested property cannot be found on an object.
    Functional interface for converting objects of a specific type to their string representation.
    Collection of standard stringifier implementations for the Bean-Centric Testing framework.
    Functional interface for pre-processing and transforming objects before conversion.
    Collection of standard swapper implementations for the Bean-Centric Testing framework.
    A supplier that throws an exception.