-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SONARJAVA-4424 add methods annotated with @Test to the list of testNg test methods #4953
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work. There is a couple of things to revisit here:
- Simplify the lambda to find test members in a TestNG class
- Clarify which test case is raising a FP here (and if it cannot be fixed, clearly mark it as an FP when semantic is incomplete)
@@ -122,6 +122,16 @@ public class TestNGClassTest { // Noncompliant | |||
private void test1() { } | |||
public static void foo() {} | |||
} | |||
|
|||
@org.testng.annotations.Test | |||
public class TestNGClassTestUseAnnotation { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change the name of the class to TestNGDoesNotRequirePublicTestMethods
or add a compliant
comment explaining why this case is not an issue
if (members.noneMatch(member -> { | ||
boolean annotatedWithTest = isTestFieldOrMethod(member); | ||
boolean publicMethod = member.isMethodSymbol() && member.isPublic() && !member.isStatic() && !"<init>".equals(member.name()); | ||
return annotatedWithTest || publicMethod; | ||
})) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we extract the second test into its own method and simplify the code?
if (members.noneMatch(member -> { | |
boolean annotatedWithTest = isTestFieldOrMethod(member); | |
boolean publicMethod = member.isMethodSymbol() && member.isPublic() && !member.isStatic() && !"<init>".equals(member.name()); | |
return annotatedWithTest || publicMethod; | |
})) { | |
if (members.noneMatch(member -> isTestFieldOrMethod(member) || isPublicInstanceMethod(member)) { |
0f6ef83
to
ced9f43
Compare
boolean annotatedWithTest = member.isMethodSymbol() && member.metadata().annotations().stream().anyMatch(isTestNgAnnotation); | ||
boolean publicMethod = member.isMethodSymbol() && member.isPublic() && !member.isStatic() && !"<init>".equals(member.name()); | ||
return annotatedWithTest || publicMethod; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we are only testing on methods because we have isMethodSymbol
in both branches. Can we return early if the member is not a method?
boolean annotatedWithTest = member.isMethodSymbol() && member.metadata().annotations().stream().anyMatch(isTestNgAnnotation); | |
boolean publicMethod = member.isMethodSymbol() && member.isPublic() && !member.isStatic() && !"<init>".equals(member.name()); | |
return annotatedWithTest || publicMethod; | |
if (!member.isMethodSymbol()) { | |
return false; | |
} | |
boolean annotatedWithTest = member.metadata().annotations().stream().anyMatch(isTestNgAnnotation); | |
boolean publicMethod = member.isPublic() && !member.isStatic() && !"<init>".equals(member.name()); | |
return annotatedWithTest || publicMethod; |
68d593c
to
1420f80
Compare
Quality Gate passedIssues Measures |
SONARJAVA-4424
Part of