FP: S2970 with AssertJ / SoftAssertions / Field Injection

The following code produces a False Positive.

The problem is similar to [SONARJAVA-3324] FP in S2970 when using JUnit 5 Soft assertions extension. - SonarSource with one difference: in SONARJAVA-3324 the SoftAssertions instance is passed as parameter to the test method, in the following example it is injected into the test class.

@org.junit.jupiter.api.extension.ExtendWith(org.assertj.core.api.junit.jupiter.SoftAssertionsExtension.class)
class JUnit5SoftAssertionsExample {
  @org.assertj.core.api.junit.jupiter.InjectSoftAssertions
  private org.assertj.core.api.SoftAssertions softly;

  @org.junit.jupiter.api.Test
  void junit5_soft_assertions_example() {
    softly.assertThat(5).isLessThan(3);
    // No need to call softly.assertAll(), this is automatically done by the SoftAssertionsExtension
  }
}

Hey there.

What products are you using (SonarQube, SonarCloud, SonarLint) and if relevant: what versions?

Hey Colin,

I first noticed it on SonarQube Developer Edition 9.3 (Build 51899).

I can reproduce it on SonarQube Community Edition Version 9.4 (Build 54424).

Hey Colin,

any updates on this?

@Colin Same here.
I’m using SonarLint 8.0.0.63273 in IntelliJ IDEA 2022.2.4 (Ultimate Edition).
Interestingly, If @ExtendWith(SoftAssertionsExtension.class) precedes any other @ExtendsWith statements, it correctly works (no false positives).

But if the statements are swapped, S2970 issues which are false positives occur.

Hello @tomyy ,

Thank you for reporting the issue.
There is already an existing ticket for handling this issue.

Hi @kota65535 ,

Thank you for reporting the issue.
I created the below example and I could not reproduce your issue.
I am on SonarLint-IntelliJ 8.1.0.63931.

package org.example;


import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@ExtendWith(org.assertj.core.api.junit.jupiter.SoftAssertionsExtension.class)
class JUnit5SoftAssertionsExample {

  SoftAssertions softly = new SoftAssertions();

  @org.junit.jupiter.api.Test
  void junit5_soft_assertions_example() {
    softly.assertThat(5).isLessThan(3);
    // No need to call softly.assertAll(), this is automatically done by the SoftAssertionsExtension
  }
}

Hi @chrislain, How did you get the version of SonarLint plugin?
The latest version available is 8.0.0.63273 as shown here.

By the way, my usage of SoftAssertionExtension was slightly wrong. I’ve updated IntelliJ Ultimate to 2022.3.3 and then fixed the code, but the result was same.


Hi @kota65535 ,

You are right about SonarLint version. I took mine from my Intellij’s setting, which is wrong:

As for the issue itself, would it be possible for you to send me the full example or a reproducer?

@chrislain The example you posted can reproduce the issue.
Is the version of the SonarLint plugin an internal release or something else? Could you downgrade the SonarLint plugin to 8.0.0.63273?

@kota65535 thanks for the feedback. I’ll investigate more on my side.

I’m jumping in just to highlight that a ticket was created to tackle the issue, since some time has passed and it could have been missed in the thread.

Thank you for the reports!