java:S2970 FP for custom assertj assertions

Using latest Sonarqube 9.7.1 I get a FP for java:S2970 Add a call to ‘assertAll’ after all ‘assertThat’ for that code:

import static foo.FooAssertions.assertThat;

  @Test
  void invalidFooBar() throws IOException {
    ...
    assertThat(getFoo(id)).isApproved();
    assertThat(getBar(id)).isDraft();
    assertThat(getFoo(otherId)).isApproved();
    assertThat(getBar(otherId)).isDraft(); <-- sonar issue here
  }

Having custom assertions as static import:

import org.assertj.core.api.Assertions;
public class FooAssertions extends Assertions {

  protected FooAssertions() {
  }

  public static FooAssert assertThat(Foo actual) {
    return new FooAssert(actual);
  }
}

public class FooAssert extends AbstractObjectAssert<FooAssert, Foo> {

  protected FooAssert(Foo foo) {
    super(foo, FooAssert.class);
  }

  public FooAssert isDraft() {
    assertThat(this.actual.getFooState().getValue()).isEqualTo(FooState.DRAFT);
    return this;
  }

  public FooAssert isApproved() {
    assertThat(this.actual.getFooState().getValue()).isEqualTo(FooState.APPROVED);
    return this;
  }

this is the official documented way how to extend assertj with custom assertions.

Kind regards,
Michael

Thanks for reporting this false-positive! :sonar:

Since all the necessary information has been included, we’ve flagged this for attention by an expert. This means that somebody will look at your report, maybe ask some follow-up questions, and try and determine if it’s really a false-positive that should be fied.

This review might be done hours, days, or even weeks from now. If it takes a while – it doesn’t mean your report isn’t important to us, it just means that our teams are already hard-at-work developing new language analysis features, and your report is in the queue.

If you’re using SonarQube or SonarCloud – an issue administrator can always mark an issue as a false-postive in the UI (this also suppresses it in SonarLint when using Connected Mode). The rule can also be disabled in your Quality Profile if it’s particularly nosy.

Hi @reitzmichnicht

Thank you for reporting!

I tried reproducing your example on SonarQube 9.7.1, and it works as expected.
Do you get FP only on the line you stated?

assertThat(getBar(otherId)).isDraft(); <-- sonar issue here

All the best,

Irina

I tried to simplify the example, we still have lot of sonar issues like this:

Can you please verify that isDraft() method is implemented correctly? Can you please provide me with a code snippet?
With the example you provided, I didn’t manage to reproduce the FP.
Thank you.

All the best,

Irina