Skip to content

Conversation

leonardo-pilastri-sonarsource
Copy link
Contributor

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource commented Sep 25, 2025

USER-1011

Part of


class AssertionsWithoutMessageCheckSample {
void foo() {
org.testng.Assert.assertTrue(true); // Noncompliant

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to org.testng.Assert.assertTrue(true, "message"); // Noncompliant and it still passes. I guess that's because the messages in testng are the last parameter.

Copy link

sonarqube-next bot commented Oct 2, 2025

}
}

private static boolean expectedMessageArgIsNotString(MethodInvocationTree mit, Type type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to clean it up: I'd avoid negatives in method names and do ! where it is callled, conditional expression, so that variables don't change, also checking index < size is more idiomatic IMO. Wdyt?

  /**
   * True if the call has a message argument. Such an argument is a string
   * and it is the first of the last argument (depending on the assertion library).
   */
  private static boolean hasMessageArg(MethodInvocationTree mit, Type type) {
    int expectedMessageArgIndex = (type.is("org.testng.Assert") || type.is("org.testng.AssertJUnit")) ? 1 : 0;
    List<ExpressionTree> args = mit.arguments();
    return expectedMessageArgIndex < args.size() && isString(args.get(expectedMessageArgIndex));
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants