+ Start a Discussion
Rst123Rst123 

Help in incresing test Coverage

I have written a class
link :- https://docs.google.com/document/d/1i3OjMQ1sPvaV0TKaKIG0O8dfHb4ILTAPie_86F66KxM/edit?usp=sharing

Test Class :-
I have written a test class for a Apex class but its not covering and showing only 5% coverage...
Can you help me in getting code coverage :-
I have highlighted the link of my CLASS below
@isTest
private class TestABC {

    static testMethod void specialReplaceAmpersandTest() {
        String testString = 'test&replacement';
        String resultString = 'test%26replacement';
        System.assertEquals(Utility.specialReplaceAmpersand(testString),resultString);
     
    }
 
    static testMethod void urlEncodeTest()
    {
        String testString = 'test&url%encode%test';
        String resultString = 'test%26url%25encode%25test';
     
        System.assertEquals(Utility.urlEncode(testString),resultString);
    }
 
    static testMethod void differentiateListsTest()
    {
        List<String> masterList = new List<String>();
        masterList.add('Element1');
        masterList.add('Element2');
        masterList.add('Element3');
        List<String> childList = new List<String>();
        childList.add('Element2');
        List<String> resultList = new List<String>();
        resultList.add('Element1');
        resultList.add('Element3');
     
        System.assertEquals(Utility.differentiateLists(masterList,childList),resultList);
     
    }
 
    static testMethod void substractSetTest()
    {
        Set<String> set1 = new Set<String>();
        set1.add('Element1');
        set1.add('Element2');
        set1.add('Element3');
        Set<String> set2 = new Set<String>();
        set2.add('Element2');
        Set<String> resultSet = new Set<String>();
        resultSet.add('Element1');
        resultSet.add('Element3');
        System.assertEquals(Utility.SubstractSet(set1,set2), resultSet);
    }

    Static testMethod void checkForNullTest()
    {
        String str1 = 'testStr1';
        String str2 = 'testStr2';
        String str3;
        Decimal decVal1 = 10.0;
        Decimal decVal2 = 11.0;
        Decimal decVal3;
        Integer intVal1 = 10;
        Integer intVal2 = 11;
        Integer intVal3;
        Boolean bool1 = true;
        Boolean bool2 = false;
        String str4 = 'T' ;
        Date dt1 = system.today();
        Date dt2 = system.today() + 1;
        Date dt3;
        DateTime datTime1 = DateTime.now();
        DateTime datTime2 = DateTime.now() + 1;
        DateTime datTime3;
     
        system.assertEquals(Utility.checkForNull(str1,str2),str2);
        system.assertEquals(Utility.checkForNull(str1,str3),str1);
        system.assertEquals(Utility.checkForNull(decVal1,decVal2),decVal2);
        system.assertEquals(Utility.checkForNull(decVal1,decVal3),decVal1);
        system.assertEquals(Utility.checkForNull(intVal1,intVal2),intVal2);
        system.assertEquals(Utility.checkForNull(intVal1,intVal3),intVal1);
        system.assertEquals(Utility.checkForNull(bool1,bool2),bool2);
        system.assertEquals(Utility.checkForNull(bool1,str4),bool1);
        system.assertEquals(Utility.checkForNull(dt1,dt2),dt2);
        system.assertEquals(Utility.checkForNull(dt1,dt3),dt1);
        system.assertEquals(Utility.checkForNull(datTime1,datTime2),datTime2);
        system.assertEquals(Utility.checkForNull(datTime1,datTime3),datTime1);
     
    }
 
   static testMethod void getPhoneEscapedTest()
   {
    String FullNumber = '203-000-007x2345';
    String phoneNumber = '203000007';
    String extension = '2345';
    System.assertEquals(Utility.getPhoneEscaped(FullNumber,true),phoneNumber);
    System.assertEquals(Utility.getPhoneEscaped(FullNumber,false),extension);
   }

   static testMethod void testEncrypt()
   {
    String Value = '003M0000008tN30';
    String Test = 'JiW2d9ABjkbJ15c0jFQg7g%3D%3D';
    System.assertEquals(Utility.EncryptId(Value),Test);
   }

   static testMethod void testEncryptIdB64()
   { 
    String Value = '003M0000008tN30';
    String Test = 'JiW2d9ABjkbJ15c0jFQg7g==';
    System.assertEquals(Utility.EncryptIdB64(Value),Test);
   }

   static testMethod void testsaveMultipleLogsToDataStore()
   { 
         integer TEST_SIZE = 5 ;
         String [] idList = new String[0] ;
         String [] strList = new String[0] ;
         List<Account> aList = new List<Account>() ;
         for (integer i=0 ;i < TEST_SIZE ;i++){
            Account testAccount = new Account(Name='Test Account',Request_Merge__c = TRUE,Account_Merge_Details__c='Test Detail');
            aList.add(testAccount) ;
         }
         insert aList;
      
         for (Account a:aList)
            idList.add(a.Id) ;
      
         for (integer i=0 ;i < TEST_SIZE ;i++){
            String str = '' ;
            for (integer k=0 ; k < 700;k++)
                 str+='AAAAAAAAAAAA ' ;
            System.debug('--- ' + str.length());  
            strList.add(str) ;
         }
        Test.startTest() ;
        System.debug('ID List size : ' + idList.size() + '  StringList size:  ' + strList.size());
        Utility.saveMultipleLogsToDataStore(idList, 'Test', strList) ;
        Integer count = [select count() from Data_Store__c where Parent_Object__c = 'Test'] ;
        System.assertEquals(TEST_SIZE*2, count) ; // String is going to be split into 2
        Test.stopTest();
     
   }

   static testMethod void testcheckLimits(){
         integer TEST_SIZE = 1 ;
         List<Account> aList = new List<Account>() ;
         for (integer i=0 ;i < TEST_SIZE ;i++){
            Account testAccount = new Account(Name='Test Account'+i,Request_Merge__c = TRUE,Account_Merge_Details__c='Test Detail'+i);
            aList.add(testAccount) ;
         }
         insert aList;
      
         String debugX = '******* Test log' ;
      
         debugX = Utility.checkLimits(debugX) ;
               
         System.assertEquals(debugX.indexof('RESOURCE USAGE')>0, true) ;
             
   }
}

Ashish_SFDCAshish_SFDC
Hi , 


The code is too long to analyse and reproduce. 

Basically in the test class we write the dummy methods which will call the main class and execute all the lines in the code. 

If we have conditional loops then we give either conditions to make sure the condition pass and fail lines of code will get executed or passed through. 

So, basically you have to insert  a dummy record for every If and Else loop to be tested. 

And I see there are lot of these conditional loops that have not been covered in the code coverage. 

Reply back if you have any specific questions. 


Regards,
Ashish