Skip to content

Conversation

@stevecl5
Copy link
Contributor

@stevecl5 stevecl5 commented Nov 19, 2025

Summary of Changes

Adds test helpers for comparing objects based on deep reflection. This is especially helpful when writing tests for object types that do not override Object.equals().

Public API Additions/Changes

Added DeepReflectionEquals:

This is a custom ArgumentMatcher used with Mockito when and verify methods. Mockito provides eq() which compares objects using equals() and deepEq which does a shallow reflection-based comparison. In contrast, deepRefEq does a deep reflection-based comparison, recursively comparing nested objects based on their internal structure.

import static com.mx.path.testing.util.DeepReflectionEquals.deepRefEq

// Verifies that 'doSomething' was called with an object structurally equal to 'expected', 
// ignoring the 'id' field
Mockito.verify(subject).doSomething(deepRefEq(expected, "id"))

Added TestUtils.deepEquals:

This is a static assertion utility that compares two objects using deep reflection. Upon mismatch, it throws an AssertionError with detailed field-by-field differences. It also guarantees that both objects are of the exact same class.

given:
def expected = new User(name: "Foo", details: new Details(value: 1))

when:
def result = subject.getUserData()

then:
// Uses deep reflection to check all fields and nested fields
TestUtils.deepEquals(result, expected)

The above example could also be written as:

then:
result instanceof User
result.name == expected.name
result.nested.value == expected.details.value

However, this can become cumbersome for objects with lots of fields and nested objects.

Downstream Consumer Impact

N/A

How Has This Been Tested?

Added unit tests to confirm all new functionality. There are no changes to existing functionality and the new methods will only be used in test classes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

@stevecl5 stevecl5 merged commit 2fa9f13 into master Nov 19, 2025
9 of 10 checks passed
@stevecl5 stevecl5 deleted the scl/test-helpers branch November 19, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants