Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

class AssertionsWithoutMessageCheckSample {
void foo() {
org.testng.Assert.assertTrue(true, "msg"); // Compliant
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.

org.testng.AssertJUnit.assertTrue(true); // Noncompliant
org.assertj.core.api.Assertions.assertThat("").usingComparator(null).as("a").isEqualTo(222); // Compliant
org.junit.Assert.assertTrue(true); // Noncompliant {{Add a message to this assertion.}}
// ^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected MethodMatchers getMethodInvocationMatchers() {
return MethodMatchers.or(
MethodMatchers.create()
.ofTypes("org.junit.jupiter.api.Assertions", "org.junit.Assert", "junit.framework.Assert", "org.fest.assertions.Fail",
"org.assertj.core.api.Fail")
"org.assertj.core.api.Fail", "org.testng.Assert", "org.testng.AssertJUnit")
.name(name -> name.startsWith(ASSERT) || "fail".equals(name)).withAnyParameters().build(),
FEST_LIKE_ABSTRACT_ASSERT
);
Expand All @@ -105,11 +105,21 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
checkFestLikeAssertion(mit, symbol, reportLocation);
} else if (type.is("org.junit.jupiter.api.Assertions")) {
checkJUnit5(mit, reportLocation);
} else if (mit.arguments().isEmpty() || !isString(mit.arguments().get(0)) || isAssertingOnStringWithNoMessage(mit)) {
} else if (mit.arguments().isEmpty() || !hasMessageArg(mit, type) || isAssertingOnStringWithNoMessage(mit)) {
reportIssue(reportLocation, MESSAGE);
}
}

/**
* 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));
}

private void checkFestLikeAssertion(MethodInvocationTree mit, Symbol symbol, IdentifierTree reportLocation) {
if (isConstructor(symbol)) {
return;
Expand Down