+ Start a Discussion
lei.zhang@zuora.comlei.zhang@zuora.com 

Code coverage issuse

Hi,

    I find a very wired question about code coverage. See the attached pic. Why the close braces not covered? The same question to the TestMethod?  It's not the browser problem, I have tried in different browsers, the results are the same.

    Can anyone help me? Thank you very much!

 



Navatar_DbSupNavatar_DbSup

Hi,

Curly braces are not counted inside the coverage of code. It considers those curly braces which is with some other syntax like in 3rd line of your code its shows because you’re for loop condition is executed and the curly braces exist inside the same line. Try the below code with 100% coverage and it don’t count curly braces inside the code coverage.

 

Test methods are use for unit testing of your apex class, trigger.

 

 

Public class AssignLeads

{

        public static Boolean assignAlreadyCalled=FALSE;

   

       public static boolean assignAlreadyCalled()

       {

        return assignAlreadyCalled;

       }

     

       @future

       public static void assign(List<Id> lIds)

       {

       assignAlreadyCalled=TRUE;

       List<Lead> leads=[SELECT Id,LastModifiedBy.Alias FROM Lead WHERE Id IN: lIds];

       for(lead l:leads)

       {

       Database.DMLOptions dmo = new Database.DMLOptions();

       dmo.assignmentRuleHeader.useDefaultRule= true;

       l.setOptions(dmo);

       }

       update(leads);

       }

       public static testmethod void testCoverage()

       {

           list<id> lds=new list<id>();

           lead l=new lead(lastname='test',company='abc');

           insert l;

           lds.add(l.id);

           AssignLeads a=new AssignLeads();

           AssignLeads.assign(lds);

           AssignLeads.assignAlreadyCalled();     

       }

     

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

lei.zhang@zuora.comlei.zhang@zuora.com

Hi, Navatar_DbSup, Thank you very much for your kindly help! But I am sorry I do not understand what you mean the 3rd line of my code, I included the line number in the bellow pic. Could you help me answer the questions that display in the pic?

    1. why the 'else' statement not covered but the 'throw new' statement covered? Does it skipped the 'else' statement?

    2. For line 105, I find the blank line is counted. Why?

Thank you!

 

 

 

 

Navatar_DbSupNavatar_DbSup

Hi,

 

Only script statements are considered for test coverage. Line break is not considered as script statement. The thumb rule of apex is “Any thing ending with semi-colon is script statement !”,  so here are the quick notes about what’s not a script statement

 

• Any Apex Standard class methods that you invoke, doesn’t matters how complex it’s internally is. It will never get counter to your script limits.

• Any curly braces { }, newlines and tabs you add are not script statements. Add them to make your code more readable.

• Any conditional evaluations/expressions for ex. if (<conditional expression>) are not counted into script statement.

lei.zhang@zuora.comlei.zhang@zuora.com

But we see that the Line break in line 105 , Curly Brace in line 102 104  and the conditional evaluations/expressions in line 100 are all red, I am afraid that means they required code coverage.

 

Are there any solutions to sovle this problem? I think these lines should not be red, but blue...

carlocarlo

Just focus on getting the lines which do include code covered and you should see the % get higher.

Naidu PothiniNaidu Pothini

Edit your class and save it so that the code coverage drops to '0'. Now run the test.

 

Even I came up with the same issue and I tried this and it worked for me.

 

Hope it works for you.

 

-Naidu

carlocarlo

You mean 100 surely?

Naidu PothiniNaidu Pothini

 

It works for the line not covered like the one above.

 

-Naidu

lei.zhang@zuora.comlei.zhang@zuora.com

Thank you very much! Naidu.

I have solved the problem, and I cannot reproduce the problem now, so I cannot try your solution. I do the bellow steps, and the problem solved:

    - go to "Set up" => "Development" => "Apex Test Execution" => "View Test History" => "clear test results"

    - run all test again

I think the test results impact the Code Coverage "%", but I donnot know why, I think this is a salesforce bug. I think it is worth noting that when I checked the test history, the number of the results overed 2000.

hideawayguyhideawayguy
naidu, I am just stunned right now - spent hours trying to figure something similar out until i stumbled on this post and moved everything to one line - granted, i'm def not an expert in test classes, but to think that this is a solution just blows my mind.... thank you anyway! #tohellwithcodecoverage