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 with Apex Testing

Hello everyone,

I need a hand getting 100% code coverage for one of my Apex Classes

The Apex Class:
public class avgCES{

    private final Case cas;

    public double avrgCES{public get; public set;}
    public string CES_Colour{public get; public set;}
    public double count{public get; public set;}

    public avgCES(ApexPages.StandardController controller) {

    this.cas = (Case)Controller.getRecord();
    

if(cas.account.id!=NULL){
    
    List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.account.id and How_easy_was_it_to_get_the_help_you_need__c!=NULL];

    double d=0.0;
    integer count=0;
    if(allCase.size()>0){
    for(case c: allCase)
    {
     d+=c.How_easy_was_it_to_get_the_help_you_need__c;
     count++;
    }
    }
    
    
    if(count>0)
    {
        avrgCES=d/count;
    }
    else
    {
        avrgCES=0;
        CES_Colour='Grey';
    }
    
    if(avrgCES<6)
    {
        CES_Colour='Red';
    }
    else if(avrgCES>=6 && avrgCES <=8 )
    {
        CES_Colour='Yellow';
    }
    else
    {
        CES_Colour='Green';
    }
}
}
}

The Test Class
@isTest
public class avgCES_test{
    public static testMethod void testAvgCES() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=6);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=5);
        insert c;
        insert c2;
        ApexPages.StandardController sc = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc);
        testme.avrgCES=5.5;
        testme.CES_Colour='Red';
        
        system.assertEquals(5.5, testme.avrgCES);
        system.assertEquals('Red', testme.CES_Colour);
  }
    
    public static testMethod void testAvgCES2() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=10);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc2 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc2);
        testme.avrgCES=9;
        
        system.assertEquals(9, testme.avrgCES);
        system.assertEquals('Green', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES3() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=7);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8);
        insert c;
        insert c2;
        ApexPages.StandardController sc3 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc3);
        testme.avrgCES=7.5;
    
        testme.CES_Colour='Yellow';
        system.assertEquals(7.5, testme.avrgCES);
        system.assertEquals('Yellow', testme.CES_Colour);
  }
    
  public static testMethod void testAvgCES4() {
        Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);
        insert a;
        Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL);
        insert c;
        insert c2;
        ApexPages.StandardController sc4 = new ApexPages.standardController(c);
        avgCES testme = new avgCES(sc4);
        testme.avrgCES=0;
        testme.CES_Colour='Grey';
        system.assertEquals(0, testme.avrgCES);
        system.assertEquals('Grey', testme.CES_Colour);
  }
    

}


Im still fairly new to Apex and would appreciate any and all help with this.

Thanks in advance!
Best Answer chosen by William Keam
manhnt.bkitmanhnt.bkit
Could you try this? in your code. Because Salesforce has some issues with  cas.Account.id
if(cas.accountId != NULL){

    List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.accountId and How_easy_was_it_to_get_the_help_you_need__c!=NULL];

Manh

All Answers

manhnt.bkitmanhnt.bkit
Could you take the sceenshot of the left? (By open this class on developer console)
William KeamWilliam Keam
Is this what you were asking for?
User-added image
manhnt.bkitmanhnt.bkit
Hi William,
Add this method into your test class :

public static testMethod void testAvgCES5() {

  Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1);

  insert a;

  Case c = new Case(subject='Test Case',AccountID= a.id ,How_easy_was_it_to_get_the_help_you_need__c=7);

  Case c2 = new Case(subject='Test Case2',AccountID=a.id,How_easy_was_it_to_get_the_help_you_need__c=8);

  insert c;

  insert c2;

  ApexPages.StandardController sc3 = new ApexPages.standardController(c);

  avgCES testme = new avgCES(sc3);

  testme.avrgCES=7.5;



  testme.CES_Colour='Yellow';

  system.assertEquals(7.5, testme.avrgCES);

  system.assertEquals('Yellow', testme.CES_Colour);

  }

Manh

William KeamWilliam Keam
Hi Manh,

Thanks for the help so far unfortunately that test only gave me 30% coverage

User-added image
manhnt.bkitmanhnt.bkit
Could you try this? in your code. Because Salesforce has some issues with  cas.Account.id
if(cas.accountId != NULL){

    List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.accountId and How_easy_was_it_to_get_the_help_you_need__c!=NULL];

Manh

This was selected as the best answer
William KeamWilliam Keam
OMG thank you so much Manh!!

100% code coverage!

Again, thank you so much for your help!