function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
William KeamWilliam Keam 

Need help to test one more line of apex

Hello,

I have an apex class which I am using to calculate the average age time taken to resolve cases.  I have written a test class for the code but I am stuck at 93% coverage, any help would be greatly appreciated!

Heres my apex class:
public class avgCase{

    private final Case cas;

    public double avrgTime{public get; public set;}
    public double count{public get; public set;}
    public double d{public get; public set;}
    
    public avgCase(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    List <case> allCase=[Select Case_Duration__c from case where accountid=:cas.accountid and status='Resolved'];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.Case_Duration__c;
     count++;
    }
    }
    if(count>0){
        avrgTime=d/count;
    }
    else
    {
        avrgTime=0;  //This is the line which is not getting code coverage
    }
    }
}

Here is my test class:
@isTest
public class avgCase_test{
    static testMethod void testAvgCase() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=15243, Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',AccountID=a.id,status='Resolved');
        insert c;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCase testMe = new avgCase(sc);
        testMe.d=1;
        testMe.count=1;
        testMe.avrgTime=1;
        ApexPages.currentPage();
        System.AssertEquals(1, testme.count);
        System.AssertEquals(testme.avrgTime, 1);
	}   
}


Thanks in advance for all the help!!

Best Answer chosen by William Keam
Andy BoettcherAndy Boettcher
So how is the value for "Case_Duration__c" being computed?  It looks like however that is being computed always has a positive value (or the collection of Cases are always a positive number).

What you COULD do would be to set "avrgTime=0;" OUTSIDE of the else conditional - put it right above the if conditional (line 23) and just remove the else completely - so avrgTIme is always 0 unless the "count>0" is met.

All Answers

Andy BoettcherAndy Boettcher
So how is the value for "Case_Duration__c" being computed?  It looks like however that is being computed always has a positive value (or the collection of Cases are always a positive number).

What you COULD do would be to set "avrgTime=0;" OUTSIDE of the else conditional - put it right above the if conditional (line 23) and just remove the else completely - so avrgTIme is always 0 unless the "count>0" is met.
This was selected as the best answer
William KeamWilliam Keam
Awesome, thanks for the help, 100% coverage achieved!