Menu

#1521 [apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <BlockStatement, BlockStatement>

PMD-5.5.1
closed
PMD
3-Major
Bug
5.5.1
apexunit.xml
2016-11-04
2016-09-06
No

While executing PMD Apex on a project I got following parse exception (below). I'm attaching class file for reference.

C:\Projects\CAS\git\cas-2.0\src\classes\C2_Assignment_Report_EOD_Schedule_Test.cls:5:   Apex unit tests should System.assert() or assertEquals() or assertNotEquals()
Exception applying rule ApexUnitTestClassShouldHaveAsserts on file C:\Projects\CAS\git\cas-2.0\src\classes\C2_Assignment_Report_Job_Test.cls, continuing with next rule
java.lang.IllegalArgumentException: expected one element but was: <BlockStatement, BlockStatement>
    at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:317)
    at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:289)
    at net.sourceforge.pmd.lang.apex.rule.apexunit.ApexUnitTestClassShouldHaveAsserts.checkForAssertStatements(ApexUnitTestClassShouldHaveAsserts.java:32)
    at net.sourceforge.pmd.lang.apex.rule.apexunit.ApexUnitTestClassShouldHaveAsserts.visit(ApexUnitTestClassShouldHaveAsserts.java:27)
    at net.sourceforge.pmd.lang.apex.ast.ASTMethod.jjtAccept(ASTMethod.java:15)
    at net.sourceforge.pmd.lang.apex.ast.AbstractApexNode.childrenAccept(AbstractApexNode.java:101)
    at net.sourceforge.pmd.lang.apex.rule.AbstractApexRule.visit(AbstractApexRule.java:132)
    at net.sourceforge.pmd.lang.apex.rule.AbstractApexRule.visit(AbstractApexRule.java:143)
    at net.sourceforge.pmd.lang.apex.rule.apexunit.AbstractApexUnitTestRule.visit(AbstractApexUnitTestRule.java:30)
    at net.sourceforge.pmd.lang.apex.rule.AbstractApexRule.visitAll(AbstractApexRule.java:121)
    at net.sourceforge.pmd.lang.apex.rule.AbstractApexRule.apply(AbstractApexRule.java:115)
    at net.sourceforge.pmd.RuleSet.apply(RuleSet.java:306)
    at net.sourceforge.pmd.RuleSets.apply(RuleSets.java:125)
    at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:146)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:76)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:43)
    at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:79)
    at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:25)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
1 Attachments

Discussion

  • Reda Benh

    Reda Benh - 2016-09-06

    This parsing error add the violation to all testmethods !!!
    I get the error message :

    Apex unit tests should System.assert() or assertEquals() or assertNotEquals()

    in all my test methods (even if the method have assert statement)

    When i check the code of this rule i see that you set by default the isAssertFound = false;
    See the code https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/apexunit/ApexUnitTestClassShouldHaveAsserts.java#L40

     private Object checkForAssertStatements(ApexNode<?> node, Object data) {
            final List<ASTBlockStatement> blockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
            final List<ASTStatement> statements = Iterables.getOnlyElement(blockStatements).findDescendantsOfType(ASTStatement.class);
            boolean isAssertFound = false; // <== Assert not found by default !! 
    
            for (final ASTStatement statement : statements) {
                final List<ASTMethodCallExpression> methodCalls = statement.findDescendantsOfType(ASTMethodCallExpression.class);
    
                for (final ASTMethodCallExpression methodCallExpression : methodCalls) {
                    final String methodName = methodCallExpression.getNode().getMethod().getName();
    
                    if (methodCallExpression.getNode().getDefiningType().getApexName().equalsIgnoreCase(SYSTEM)
                        && (methodName.equalsIgnoreCase(ASSERT)
                            || methodName.equalsIgnoreCase(ASSERT_EQUALS)
                            || methodName.equalsIgnoreCase(ASSERT_NOT_EQUALS))) {
                        isAssertFound = true;
                    }
                }
            }
    
            if (!isAssertFound) {
                addViolation(data, node);
            }
    
            return data;
        }
    

    Thanks for your help

     

    Last edit: Reda Benh 2016-09-06
  • Andreas Dangel

    Andreas Dangel - 2016-11-04
    • summary: Parsing error on APEX class: expected one element but was: <blockstatement, blockstatement=""> --> [apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <blockstatement, blockstatement=""></blockstatement,></blockstatement,>
    • status: open --> in-progress
    • assigned_to: Andreas Dangel
     
  • Andreas Dangel

    Andreas Dangel - 2016-11-04
    • status: in-progress --> closed
     
  • Reda Benh

    Reda Benh - 2016-11-04

    Thanks Andreas,
    Can you please tell when you will release version 5.5.2 ?

    Thanks

     
    • Andreas Dangel

      Andreas Dangel - 2016-11-04

      Soon - today or tomorrow :)

       

Log in to post a comment.