Skip to content

Commit c2b7487

Browse files
Review changes
1 parent b6489e6 commit c2b7487

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

java-checks/src/main/java/org/sonar/java/checks/tests/AssertionsWithoutMessageCheck.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,19 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
105105
checkFestLikeAssertion(mit, symbol, reportLocation);
106106
} else if (type.is("org.junit.jupiter.api.Assertions")) {
107107
checkJUnit5(mit, reportLocation);
108-
} else if (mit.arguments().isEmpty() || expectedMessageArgIsNotString(mit, type) || isAssertingOnStringWithNoMessage(mit)) {
108+
} else if (mit.arguments().isEmpty() || !hasMessageArg(mit, type) || isAssertingOnStringWithNoMessage(mit)) {
109109
reportIssue(reportLocation, MESSAGE);
110110
}
111111
}
112112

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

122123
private void checkFestLikeAssertion(MethodInvocationTree mit, Symbol symbol, IdentifierTree reportLocation) {

0 commit comments

Comments
 (0)