# java:S2970 FP for custom assertj assertions

**URL:** <https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740>\
**Category:** Report False-positive / False-negative...\
**Tags:** sonarqube, java\
**Created:** [December 12, 2022, 8:36am UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740 "2022-12-12T08:36:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reitzmichnicht](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/reitzmichnicht/32/288_2.png) [@reitzmichnicht](https://community.sonarsource.com/u/reitzmichnicht)\
**Post date:** [December 12, 2022, 8:36am UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740/1 "2022-12-12T08:36:13Z")

</div>

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

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [December 13, 2022, 9:33am UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740/3 "2022-12-13T09:33:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![irina.batinic](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/irina.batinic/32/38681_2.png) [@irina.batinic](https://community.sonarsource.com/u/irina.batinic)\
**Post date:** [January 16, 2023, 1:20pm UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740/8 "2023-01-16T13:20:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reitzmichnicht](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/reitzmichnicht/32/288_2.png) [@reitzmichnicht](https://community.sonarsource.com/u/reitzmichnicht)\
**Post date:** [January 16, 2023, 2:26pm UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740/9 "2023-01-16T14:26:19Z")

</div>

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

 ![2023-01-16 15_25_08-Add a call to 'assertAll' after all 'assertThat'. - Vivaldi](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/8/b/8bef3987cff3a184e9b622faf4159408379cf9da.png)

---

<div class="post-metadata">

**Author:** ![irina.batinic](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/irina.batinic/32/38681_2.png) [@irina.batinic](https://community.sonarsource.com/u/irina.batinic)\
**Post date:** [January 17, 2023, 3:00pm UTC](https://community.sonarsource.com/t/java-s2970-fp-for-custom-assertj-assertions/78740/10 "2023-01-17T15:00:16Z")

</div>

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
