Skip to content

usingRecursiveComparison() not working when Class implements Iterable #2812

Description

@linksblackmask

Summary

When asserting 2 objects are equal usingRecursiveComparison(), when the class implements Iterable, seems to cause an equals check only the iterable objects, and not the parent object. Noticed this in a more complicated Class, so mocked up an example.

In the below example, the assert in the test passes, even when the objects aren't equal. Having followed through the code during debug, it seems if the class implements Iterable, the checks for all other variables are not performed. Tested using assertj version 3.23.1.

Example

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class SomeTest {
  public static class SomeEntry {
    private String a;

    public SomeEntry(String a) {
      this.a = a;
    }
  }

  public static class SomeIterable implements Iterable<SomeEntry>{
    private final List<SomeEntry> entries = new ArrayList<>();
    private final String name;

    public SomeIterable(String name) {
      this.name = name;
    }

    @Override
    public Iterator<SomeEntry> iterator() {
      return entries.iterator();
    }
  }


  @Test
  public void test() {
    assertThat(new SomeIterable("bob")).usingRecursiveComparison().isEqualTo(new SomeIterable("dave"));
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions