Skip to content

Inconsistent Collection Behaviour Between Records and Classes #125

@jk-idealo

Description

@jk-idealo

I know, that due to the currently supported java-version, Fixture cannot natively support records. However, so far it implicitly supports them through constructor-creation in the case that an instance cannot be mutated after creation.

This test is green:

@Test
void canCreateDistinctElementsInCollectionForClasses() {
    var actual = fixture().create(ClassWithList.class);

    assertThat(actual.getChildren().get(0)).isNotSameAs(actual.getChildren().get(1));
}

public static class ClassWithList {
    private final List<Child> children;

    public ClassWithList(List<Child> children) {
        this.children = children;
    }

    public List<Child> getChildren() {
        return children;
    }

    public static class Child {
        private final String string;

        public Child(String string) {
            this.string = string;
        }

        public String getString() {
            return string;
        }
    }
}

The test above is green, because Fixture will create distinct instances of classes in a collection.

The test below however is red, because the behaviour above does not seem to apply to records:

@Test
void canCreateDistinctElementsInCollectionForRecords() {
    var actual = fixture().create(RecordWithList.class);

    assertThat(actual.children().get(0)).isNotSameAs(actual.children().get(1));
}

public record RecordWithList(List<Child> children) {
    public record Child(String string) {}
}

(Unfortunately, I cannot run the test in Fixture, since its java-version doesn't support records.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions