Have you tried to resolve this issue yourself first?
Bug Description
Hello All,
Our Development team tried to crate one custom ruleset to identify if any test method does not have Test.starTest() and Test.stopTest() declaration but it did not worked. We are not getting any error.
Please note that our team referred below GitHub link as well.
https://github.com/starch-uk/sca-extra
Please see below for the TestMethodsMustUseStartStopTest.xml file as well as code-analyzer1.yaml file. Please review and help us in resolving issue. We need to resolve this issue on priority.
code-analyzer1.yaml
engines:
pmd:
disable_engine: false
file_extensions:
apex:
- .apex
- .cls
- .trigger
custom_rulesets:
- rulesets/bestpractices/FinalVariablesMustBeFinal.xml
- rulesets/bestpractices/RegexPatternsMustBeStaticFinal.xml
- rulesets/bestpractices/StaticMethodsMustBeStatic.xml
- rulesets/bestpractices/StaticVariablesMustBeFinalAndScreamingSnakeCase.xml
- rulesets/bestpractices/TestClassIsParallel.xml
- rulesets/codestyle/AvoidMagicNumbers.xml
- rulesets/codestyle/AvoidOneLinerMethods.xml
- rulesets/codestyle/InnerClassesMustBeOneWord.xml
- rulesets/codestyle/MapShouldBeInitializedWithValues.xml
- rulesets/codestyle/MultipleStringContainsCalls.xml
- rulesets/codestyle/NoMethodCallsAsArguments.xml
- rulesets/codestyle/NoMethodCallsInConditionals.xml
- rulesets/codestyle/NoSingleLetterVariableNames.xml
- rulesets/codestyle/PreferBuilderPatternChaining.xml
- rulesets/codestyle/PreferConcatenationOverStringJoinWithEmpty.xml
- rulesets/codestyle/PreferMethodCallsInLoopConditions.xml
- rulesets/codestyle/PreferNullCoalescingOverTernary.xml
- rulesets/codestyle/PreferSafeNavigationOperator.xml
- rulesets/codestyle/PreferStringJoinOverConcatenation.xml
- rulesets/codestyle/PreferStringJoinOverMultipleNewlines.xml
- rulesets/codestyle/PreferStringJoinWithSeparatorOverEmpty.xml
- rulesets/codestyle/SingleArgumentMustBeSingleLine.xml
- rulesets/codestyle/VariablesMustNotShareNamesWithClasses.xml
- rulesets/design/AvoidLowValueWrapperMethods.xml
- rulesets/design/AvoidTrivialPropertyGetters.xml
- rulesets/design/ClassesMustHaveMethods.xml
- rulesets/design/CombineNestedIfStatements.xml
- rulesets/design/EnumMinimumValues.xml
- rulesets/design/InnerClassesCannotBeStatic.xml
- rulesets/design/InnerClassesCannotHaveStaticMembers.xml
- rulesets/design/NoCustomParameterObjects.xml
- rulesets/design/NoInterfacesEndingWithCallback.xml
- rulesets/design/NoParameterClasses.xml
- rulesets/design/NoThisOutsideConstructors.xml
- rulesets/design/NoUnnecessaryAttributeVariables.xml
- rulesets/design/NoUnnecessaryReturnVariables.xml
- rulesets/design/UnusedVariableNotReturnedOrPassed.xml
- rulesets/design/PreferPropertySyntaxOverGetterMethods.xml
- rulesets/design/PreferSwitchOverIfElseChains.xml
- rulesets/design/SingleParameterMustBeSingleLine.xml
- rulesets/design/DmlInLoopAcrossMethods.xml
- rulesets/design/TestMethodsMustUseStartStopTest.xml
- rulesets/documentation/ExceptionDocumentationRequired.xml
- rulesets/documentation/MethodsRequireExampleTag.xml
- rulesets/documentation/ProhibitAuthorSinceVersionTags.xml
- rulesets/documentation/SingleLineDocumentationFormat.xml
- rulesets/documentation/ValidGroupTagValues.xml
regex:
custom_rules:
NoConsecutiveBlankLines:
regex: /\n\s*\n\s*\n/g
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prevents two or more consecutive blank lines in Apex code.
Code should have at most one blank line between statements,
methods, or other code elements.'
violation_message:
'Two or more consecutive blank lines are not allowed. Use at
most one blank line between statements.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
ProhibitSuppressWarnings:
regex: /@SuppressWarnings([^)])|//\sNOPMD/gi
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prohibits the use of @SuppressWarnings annotations and
NOPMD comments in Apex code. Suppressions hide code quality
issues; prefer fixing the underlying problems or improving
rules instead.'
violation_message:
'Suppression of warnings is not allowed. Fix the underlying
issue or improve the rule instead of suppressing violations.'
severity: 'High'
tags: ['CodeStyle', 'Recommended']
NoLongLines:
regex: /.{81,}/gm
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Enforces a maximum line length of 80 characters. Lines
longer than 80 characters reduce readability and make code
harder to review. Use shorter class, attribute, method, and
variable names to improve readability.'
violation_message:
'Line exceeds 80 characters. Use shorter class, attribute,
method, and variable names to improve readability.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
ProhibitPrettierIgnore:
regex: ///\s*prettier-ignore/gi
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prohibits the use of prettier-ignore comments in Apex code.
Code should be formatted consistently without exceptions.
Using prettier-ignore comments undermines code formatting
standards and makes the codebase less maintainable.'
violation_message:
'Prettier-ignore comments are not allowed. Code should be
formatted consistently without exceptions.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
TestMethodsMustUseStartStopTest.xml
<ruleset
name="Apex Design Rules"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"
<description>
Custom Salesforce PMD rules to enforce usage of Test.startTest() and Test.stopTest() in all test methods
</description>
<!-- Rule: Missing Test.startTest() or Test.stopTest() -->
<rule name="TestMethodsMustHaveExactlyOneStartStopTest"
language="apex"
message="Test methods must contain exactly one Test.startTest() and one Test.stopTest()."
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
<description>
Ensures every test method has exactly one Test.startTest() and one Test.stopTest().
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[
(
.//Annotation/Name[
lower-case(@Image)='istest'
or lower-case(@Image)='testmethod'
]
)
and
(
count(.//MethodCall[
.//Name[lower-case(@Image)='test.starttest']
]) != 1
or
count(.//MethodCall[
.//Name[lower-case(@Image)='test.stoptest']
]) != 1
)
]
]]>
</value>
</property>
</properties>
I am not able to upload files due to company policy. So added both files directly in description.
Output / Logs
Steps To Reproduce
We ran PMd Scan in our SFOrg. But could not see any Test class where there are Testmethod without Test.startTest(); and Test.stopTest();
Expected Behavior
We are expecting solution to this issue
Operating System
Windows
Salesforce CLI Version
Latest CLI used by Our Development Team
Code Analyzer Plugin (code-analyzer) Version
code-analyzer 5.13.0
Node Version
No response
Java Version
No response
Python Version
No response
Additional Context (Screenshots, Files, etc)
No response
Workaround
No response
Urgency
High
Have you tried to resolve this issue yourself first?
Bug Description
Hello All,
Our Development team tried to crate one custom ruleset to identify if any test method does not have Test.starTest() and Test.stopTest() declaration but it did not worked. We are not getting any error.
Please note that our team referred below GitHub link as well.
https://github.com/starch-uk/sca-extra
Please see below for the TestMethodsMustUseStartStopTest.xml file as well as code-analyzer1.yaml file. Please review and help us in resolving issue. We need to resolve this issue on priority.
code-analyzer1.yaml
engines:
pmd:
disable_engine: false
file_extensions:
apex:
- .apex
- .cls
- .trigger
custom_rulesets:
- rulesets/bestpractices/FinalVariablesMustBeFinal.xml
- rulesets/bestpractices/RegexPatternsMustBeStaticFinal.xml
- rulesets/bestpractices/StaticMethodsMustBeStatic.xml
- rulesets/bestpractices/StaticVariablesMustBeFinalAndScreamingSnakeCase.xml
- rulesets/bestpractices/TestClassIsParallel.xml
- rulesets/codestyle/AvoidMagicNumbers.xml
- rulesets/codestyle/AvoidOneLinerMethods.xml
- rulesets/codestyle/InnerClassesMustBeOneWord.xml
- rulesets/codestyle/MapShouldBeInitializedWithValues.xml
- rulesets/codestyle/MultipleStringContainsCalls.xml
- rulesets/codestyle/NoMethodCallsAsArguments.xml
- rulesets/codestyle/NoMethodCallsInConditionals.xml
- rulesets/codestyle/NoSingleLetterVariableNames.xml
- rulesets/codestyle/PreferBuilderPatternChaining.xml
- rulesets/codestyle/PreferConcatenationOverStringJoinWithEmpty.xml
- rulesets/codestyle/PreferMethodCallsInLoopConditions.xml
- rulesets/codestyle/PreferNullCoalescingOverTernary.xml
- rulesets/codestyle/PreferSafeNavigationOperator.xml
- rulesets/codestyle/PreferStringJoinOverConcatenation.xml
- rulesets/codestyle/PreferStringJoinOverMultipleNewlines.xml
- rulesets/codestyle/PreferStringJoinWithSeparatorOverEmpty.xml
- rulesets/codestyle/SingleArgumentMustBeSingleLine.xml
- rulesets/codestyle/VariablesMustNotShareNamesWithClasses.xml
- rulesets/design/AvoidLowValueWrapperMethods.xml
- rulesets/design/AvoidTrivialPropertyGetters.xml
- rulesets/design/ClassesMustHaveMethods.xml
- rulesets/design/CombineNestedIfStatements.xml
- rulesets/design/EnumMinimumValues.xml
- rulesets/design/InnerClassesCannotBeStatic.xml
- rulesets/design/InnerClassesCannotHaveStaticMembers.xml
- rulesets/design/NoCustomParameterObjects.xml
- rulesets/design/NoInterfacesEndingWithCallback.xml
- rulesets/design/NoParameterClasses.xml
- rulesets/design/NoThisOutsideConstructors.xml
- rulesets/design/NoUnnecessaryAttributeVariables.xml
- rulesets/design/NoUnnecessaryReturnVariables.xml
- rulesets/design/UnusedVariableNotReturnedOrPassed.xml
- rulesets/design/PreferPropertySyntaxOverGetterMethods.xml
- rulesets/design/PreferSwitchOverIfElseChains.xml
- rulesets/design/SingleParameterMustBeSingleLine.xml
- rulesets/design/DmlInLoopAcrossMethods.xml
- rulesets/design/TestMethodsMustUseStartStopTest.xml
- rulesets/documentation/ExceptionDocumentationRequired.xml
- rulesets/documentation/MethodsRequireExampleTag.xml
- rulesets/documentation/ProhibitAuthorSinceVersionTags.xml
- rulesets/documentation/SingleLineDocumentationFormat.xml
- rulesets/documentation/ValidGroupTagValues.xml
regex:
custom_rules:
NoConsecutiveBlankLines:
regex: /\n\s*\n\s*\n/g
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prevents two or more consecutive blank lines in Apex code.
Code should have at most one blank line between statements,
methods, or other code elements.'
violation_message:
'Two or more consecutive blank lines are not allowed. Use at
most one blank line between statements.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
ProhibitSuppressWarnings:
regex: /@SuppressWarnings([^)])|//\sNOPMD/gi
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prohibits the use of @SuppressWarnings annotations and
NOPMD comments in Apex code. Suppressions hide code quality
issues; prefer fixing the underlying problems or improving
rules instead.'
violation_message:
'Suppression of warnings is not allowed. Fix the underlying
issue or improve the rule instead of suppressing violations.'
severity: 'High'
tags: ['CodeStyle', 'Recommended']
NoLongLines:
regex: /.{81,}/gm
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Enforces a maximum line length of 80 characters. Lines
longer than 80 characters reduce readability and make code
harder to review. Use shorter class, attribute, method, and
variable names to improve readability.'
violation_message:
'Line exceeds 80 characters. Use shorter class, attribute,
method, and variable names to improve readability.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
ProhibitPrettierIgnore:
regex: ///\s*prettier-ignore/gi
file_extensions: ['.apex', '.cls', '.trigger']
description:
'Prohibits the use of prettier-ignore comments in Apex code.
Code should be formatted consistently without exceptions.
Using prettier-ignore comments undermines code formatting
standards and makes the codebase less maintainable.'
violation_message:
'Prettier-ignore comments are not allowed. Code should be
formatted consistently without exceptions.'
severity: 'Moderate'
tags: ['CodeStyle', 'Recommended']
TestMethodsMustUseStartStopTest.xml
<ruleset
name="Apex Design Rules"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"
I am not able to upload files due to company policy. So added both files directly in description.
Output / Logs
Steps To Reproduce
We ran PMd Scan in our SFOrg. But could not see any Test class where there are Testmethod without Test.startTest(); and Test.stopTest();
Expected Behavior
We are expecting solution to this issue
Operating System
Windows
Salesforce CLI Version
Latest CLI used by Our Development Team
Code Analyzer Plugin (code-analyzer) Version
code-analyzer 5.13.0
Node Version
No response
Java Version
No response
Python Version
No response
Additional Context (Screenshots, Files, etc)
No response
Workaround
No response
Urgency
High