Skip to content

Allow to ignore check on compared fields in the recursive comparison (initial issue description was: Problems on method 'usingRecursiveFieldByFieldElementComparatorOnFields') #3806

@damianopezzotti

Description

@damianopezzotti

Describe the bug
From version 3.25.0, it seems that method usingRecursiveFieldByFieldElementComparatorOnFields on AbstractIterableAssert returns an error if one of the fields don't exist. The problem is that I have polymorphic objects.

  • assertj core version: 3.25.0
  • java version: 21
  • test framework version: junit 5.11.4
  • os (if relevant):

Test case reproducing the bug

Add a test case showing the bug that we can run

@Data
@SuperBuilder
public abstract sealed class AppliedExemptionResponse permits AppliedTotalExemptionResponse, AppliedPartialExemptionResponse {
	String exemptionCode;
	String description;
	String chargeItemCode;
	String description;
}

@Value
@SuperBuilder
public class AppliedTotalExemptionResponse extends AppliedExemptionResponse {
}

@Value
@SuperBuilder
public class AppliedPartialExemptionResponse extends AppliedExemptionResponse {
	BigDecimal value;
	String type;
	boolean replacement;
}

public class MyTest {

	@Test
	void test() {

		final List<AppliedExemptionResponse> appliedExemptions = List.of(
				AppliedTotalExemptionResponse.builder().exemptionCode( "E1" ).build(),
				AppliedPartialExemptionResponse.builder().value( BigDecimal.ZERO ).build()
		);
		final List<AppliedExemptionResponse> expectedAppliedExemptions = List.of(
				AppliedTotalExemptionResponse.builder().exemptionCode( "E1" ).build(),
				AppliedPartialExemptionResponse.builder().value( BigDecimal.ZERO ).build()
		);

		assertThat( appliedExemptions )
				.usingRecursiveFieldByFieldElementComparatorOnFields( "exemptionCode", "chargeItemCode", "description", "value" )
				.containsExactlyInAnyOrderElementsOf( expectedAppliedExemptions );
	}
}

The test fails with the following error:

java.lang.IllegalArgumentException: The following fields don't exist: {value}

How can I fix the problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions