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
RadDude89RadDude89 

Increasing code coverage for class to 100%

Hi,

We have an apex class on our organistaion that has a code coverage of 88% (23/26).  The class itself is only 37 lines and the test class is 25 lines.

Apex Class:

public class Enrg_MeterOfferTag_ACL{
    public static integer rec=0;
    public static void taggOS(Set<Id> setOS, Map<Id,Id> mapOS){
        if(rec==0){
            List<Offer_Site__c> listOS=new List<Offer_Site__c>();
            List<Offer_Site__c> updOs=new List<Offer_site__c>();
            listOS=[Select id,Meter_Provided__c,Meter_Read__c from Offer_Site__c where Id in :setOS];
            for(Offer_site__c objOS:listOS){
                if(objOS.Meter_Read__c==null && mapOS.ContainsKey(objOS.Id)){
                    objOS.Meter_Provided__c=true;
                    objOS.Meter_Read__c=mapOS.get(objOS.Id);
                    updOS.add(objOS);
                }
            }
            if(updOS.Size()>0)
                update updOS;
            rec++;   
        }
    }
     public static void taggRej(Set<String> setreg, Map<String,ID> mapreg){
        if(rec==0){
            List<Registrations__c> listreg=new List<Registrations__c>();
            List<Registrations__c> updreg=new List<Registrations__c>();
            listreg=[Select id,Meter_Read_Information__c from Registrations__c where Id in :setreg];
            for(Registrations__c objreg:listreg){
                System.debug(objreg.Id);
                if(mapreg.ContainsKey(String.valueof(objreg.Id).substring(0,15))){
                    objreg.Meter_Read_Information__c=mapreg.get(String.valueof(objreg.Id).substring(0,15));
                    updreg.add(objreg);
                }
            }
            if(updreg.Size()>0)
                update updreg;
            rec++;   
        }
    }
}

 

Apex Class Test:

@isTest
private class Enrg_MeterOfferTag_ACL_Test {
private static integer rec=0;
    static testMethod void Enrg_MeterOfferTag_ACL_Test_testmethod() {
        offer__c off=new offer__c();
        insert off;
        Offer_Site__c offsite1=new Offer_Site__c(name='offersite1',offer__c=off.id,MPRN__c='23652013036', Meter_Provided__c=true);
        insert offsite1;       
        Meter_Read__c meter1=new Meter_Read__c(Offer_Site_Id__c=offsite1.id,MPRN__c='23652013036');
        insert meter1;
       
    }
    static testMethod void Enrg_MeterOfferTag_ACL_Test_testmethod1() {
       
        offer__c off1=new offer__c();
        insert off1;
        Offer_Site__c offsite2=new Offer_Site__c(name='offersite2',offer__c=off1.id,MPRN__c='23652013036');
        insert offsite2;
        Registrations__c reg=new Registrations__c() ;
        insert reg;    
        Meter_Read__c meter2=new Meter_Read__c(registration__c=reg.id);
        insert meter2;
        //update reg;
    }
}

 

Does anyone know what I could add in order to have the code coverage as 100% (26/26)?
Any help is much appreciated.

Thanks

cloudSavvyProgcloudSavvyProg
Hi RadDude89,

Your class methods are not called from the test classes. Are they called from triggers or somewhere else?

You can test your class using developer console which gives you and idea of lines that are not covered to achieve 100% coverage.

Here is the link to run the test in developer console:
https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tab_tests.htm&language=en

Here is the link to know the line that are not covered by test.
https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en_US

To deploy the code you should have 75% code coverage. But it is best practise to have 100% code coverage with some system asserts to see your code is working as intended.

Hope this helps.

Regards,
CloudSavvyProg