Warning RSPEC-5853 when using assertJ and map on OptionalAssert

Hello,

I think there may be a false positive with RSPEC-5853 when using assertJ map method with this kind of code :

        assertThat(oRecord).map(rec -> rec.getSubRecords1())
                .hasValueSatisfying(list -> {
                    assertThat(list).isNotEmpty();
                    assertThat(list).containsExactlyInAnyOrderElementsOf(s1);
                });
        assertThat(oRecord).map(Record::getSubRecords2)
                .hasValueSatisfying(list -> {
                    assertThat(list).isNotEmpty();
                    assertThat(list).containsExactlyInAnyOrderElementsOf(s2);
                });
  • SonarQube Enterprise EditionVersion 8.6 (build 39681)
  • SonarLint Intellij 4.13.0.24781

Minimal code sample to reproduce:

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

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

class RSPEC5853MapTest {

    @Test
    void issueWithRSPEC5853WhenUsingMap() {
        List<SubRecord> s1 = Arrays.asList(new SubRecord("s1"));
        List<SubRecord> s2 = Arrays.asList(new SubRecord("s2"));
        Record record = new Record(s1, s2);
        Optional<Record> oRecord = Optional.of(record);

        assertThat(oRecord).map(Record::getSubRecords1)
                .hasValueSatisfying(list -> assertThat(list).containsExactlyInAnyOrderElementsOf(s1));
        assertThat(oRecord).map(Record::getSubRecords2)
                .hasValueSatisfying(list -> assertThat(list).containsExactlyInAnyOrderElementsOf(s2));
    }

    private static class Record {
        List<SubRecord> subRecords1;
        List<SubRecord> subRecords2;

        public Record(List<SubRecord> subRecords1, List<SubRecord> subRecords2) {
            this.subRecords1 = subRecords1;
            this.subRecords2 = subRecords2;
        }

        public List<SubRecord> getSubRecords1() {
            return subRecords1;
        }

        public List<SubRecord> getSubRecords2() {
            return subRecords2;
        }
    }

    private static class SubRecord {
        private String name;

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

        public String getName() {
            return name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            SubRecord subRecord = (SubRecord) o;
            return Objects.equals(getName(), subRecord.getName());
        }

        @Override
        public int hashCode() {
            return Objects.hash(getName());
        }
    }
}

Hello @NorthernKgalagadi, and welcome to this community!

Thanks for this clear and complete reproducer. I agree that, even if it is possible to somehow merge the two assertions, this will definitely not result in something cleaner than the current solution.

Ticket created: SONARJAVA-3678.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.