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
Brad007Brad007 

Help Increase test class code coverage from 68%

 

I have marked in red the lines of code where it sayd 0% coverage. please suggest.

public class HFSendSurveyClass { public void SendSurvey() { //String hfsurvey; Date hfsurvey; //Get the most recently created config object. Config_Object__c config = [Select HFSurveyCode__c, SurveyDate__C, SOQL_Query__C from Config_Object__C where Name ='HostFamily_Survey' limit 1 ]; List<Opportunity> oppList = Database.query(config.SOQL_Query__c); //System.debug('Bharath:' +opplist); List<Account> accList = new List<Account>(); //System.debug('Bharath:' +accList); try { for(Opportunity opp:oppList) { if(opp.AccountId!= null) { Account acc = opp.Account; if(opp.Survey_Opp_Id_Match__c== 'No') { System.debug('Bharath date: '+config.SurveyDate__c); if(String.valueOf(config.SurveyDate__c) != Null) { if(String.valueOf(config.SurveyDate__c).contains('+')) { string s = String.valueOf(config.SurveyDate__c); string intvalue = adddaystostringvalue(s); hfsurvey = Date.today().addDays(integer.valueOf(intvalue)); } else { hfsurvey = Date.Today(); } } string oppcode= opp.Id; acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); acc.Survey_Dt__c = hfsurvey; acc.Survey_Opp_Id__c = oppcode.subString(0,15); System.debug('Bharath account123: '+acc.Survey_Code__c+' '+String.valueOf(config.HFSurveyCode__c)); } else if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c)) { System.debug('Bharath date2: '+config.SurveyDate__c); if(String.valueOf(config.SurveyDate__c) != Null) { if(String.valueOf(config.SurveyDate__c).contains('+')) { string s = String.valueOf(config.SurveyDate__c); string intvalue = adddaystostringvalue(s); hfsurvey = Date.today().addDays(integer.valueOf(intvalue)); } else { hfsurvey = Date.Today(); } } string oppcode= opp.Id; acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); acc.Survey_Dt__c = hfsurvey; acc.Survey_Opp_Id__c = oppcode.subString(0,15); } accList.add(acc); } } update accList; } catch (Exception e) { System.debug(e.getMessage()); } } private String adddaystostringvalue (string surveydays) { if(surveydays != null) surveydays = surveydays.substring(surveydays.indexOf('+')+ 1).trim(); return surveydays; }

 

TEst Class


	@istest

private class HFSendSurveyTestClass {


     static testMethod void testSendSurvey(){
        String query = 'Select o.Id,o.Survey_Opp_Id_Match__c ,o.Account.Name From Opportunity o where o.StageName=&#Active&# AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60 AND o.Survey_Opp_Id_Match__c=&#No&# limit 1';
        Config_Object__c config = new Config_Object__c();
        config.HFSurveyCode__c ='HF1MoArv';
        config.SurveyDate__c = 'Today';
        config.SOQL_Query__c = query;
        config.Name = 'HostFamily_Survey';
        
        Opportunity opp = new Opportunity();
        Account acc = new Account();
        
        acc.Name = 'TestSurvey';
        acc.Survey_Code__c='HFReferal';
        acc.Survey_Dt__c = Date.today();
        acc.Survey_Opp_Id__c = opp.Id;
        System.debug('Bharath acc:' +acc);
        Insert acc;
        
        opp.Name = acc.Name;
        opp.AccountId = acc.Id;
        opp.StageName = 'Active';
        Date placedate = Date.newInstance(2011,03,01);
        opp.Placmnt_Start_Date_Arrive__c = placedate;
        opp.CloseDate = Date.today();
        //opp.Account = acc;
        //opp.Survey_Opp_Id_Match__c = 'Yes';
        //opp.Survey_Opp_Id_Match__c == 'No';
        System.debug('Bharath opp:' +opp);
        Insert opp;
        
        // List<Opportunity> o = [Select Name,AccountId,StageName,Placmnt_Start_Date_Arrive__c FROM Opportunity where Name = 'TestSurvey'];
        //List<Account> a = [Select Name,Survey_Code__c,Survey_Opp_Id__c,Survey_Dt__c  FROM Account where Name = 'TestSurvey'];
        
        HFSendSurveyClass sv = new HFSendSurveyClass();
        sv.SendSurvey(); 
     }
     
     //public static testMethod void testadddaystostringvalue()
     //{
     // string x = string.valueOf('Today+2');
     // HFSendSurveyClass s = new HFSendSurveyClass();
     // s.adddaystostringvalue(x); 
        //adddaystostringvalue(x);
     //}
     
     
}

 

 

bob_buzzardbob_buzzard

Unfortunately the marking in red doesn't seem to have made it through to your post.

Brad007Brad007

 

I have marked in red the lines of code where it sayd 0% coverage. please suggest.

public class HFSendSurveyClass {
    public void SendSurvey()
    {
        
      //String hfsurvey;
        Date hfsurvey;        
      //Get the most recently created config object.
     Config_Object__c config = [Select HFSurveyCode__c, SurveyDate__C, SOQL_Query__C from Config_Object__C 
                                where Name ='HostFamily_Survey' limit 1 ];
       
     List<Opportunity> oppList = Database.query(config.SOQL_Query__c); 
     //System.debug('Bharath:' +opplist);
     List<Account> accList = new List<Account>(); 
     //System.debug('Bharath:' +accList); 
   try {             
               for(Opportunity opp:oppList)
                       
                     {
                        if(opp.AccountId!= null)
                            {
                                  Account acc = opp.Account; 
                                  if(opp.Survey_Opp_Id_Match__c== 'No')
                                      {
                                        System.debug('Bharath date: '+config.SurveyDate__c);
                                        if(String.valueOf(config.SurveyDate__c) != Null)
                                          {
                                              if(String.valueOf(config.SurveyDate__c).contains('+'))
                                              {
                                                string s = String.valueOf(config.SurveyDate__c);
                                                string intvalue = adddaystostringvalue(s);
                                                hfsurvey = Date.today().addDays(integer.valueOf(intvalue));
                                              }
                                              else 
                                              {
                                               hfsurvey = Date.Today();
                                               }
                                          }
                                            
                                             string oppcode= opp.Id;
                                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); 
                                             acc.Survey_Dt__c = hfsurvey;
                                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);               
                                             System.debug('Bharath account123: '+acc.Survey_Code__c+'      '+String.valueOf(config.HFSurveyCode__c));           
                                       }
                                                
                                 else if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c))    
                                    {                    
                                        System.debug('Bharath date2: '+config.SurveyDate__c);
                                          if(String.valueOf(config.SurveyDate__c) != Null)
                                          {
                                              if(String.valueOf(config.SurveyDate__c).contains('+'))
                                              {
                                                string s = String.valueOf(config.SurveyDate__c);
                                                string intvalue = adddaystostringvalue(s);
                                                hfsurvey = Date.today().addDays(integer.valueOf(intvalue));
                                              }
                                              else 
                                              {
                                               hfsurvey = Date.Today();
                                               }
                                          }
                                         string oppcode= opp.Id;
                                         acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); 
                                         acc.Survey_Dt__c = hfsurvey;
                                         acc.Survey_Opp_Id__c = oppcode.subString(0,15);                             
                                         
                                   } 
                              
                             accList.add(acc);                
                            }
                         }
                   update accList;                            
             } 
         
     
     catch (Exception e)
         {
           
            System.debug(e.getMessage());
        }   
     }
     private String adddaystostringvalue (string surveydays)
     {
        if(surveydays != null)
            surveydays = surveydays.substring(surveydays.indexOf('+')+ 1).trim();
            return surveydays;            
        
     }
     

 

TEst Class


	@istest

private class HFSendSurveyTestClass {


     static testMethod void testSendSurvey(){
        String query = 'Select o.Id,o.Survey_Opp_Id_Match__c ,o.Account.Name From Opportunity o where o.StageName=&#Active&# AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60 AND o.Survey_Opp_Id_Match__c=&#No&# limit 1';
        Config_Object__c config = new Config_Object__c();
        config.HFSurveyCode__c ='HF1MoArv';
        config.SurveyDate__c = 'Today';
        config.SOQL_Query__c = query;
        config.Name = 'HostFamily_Survey';
        
        Opportunity opp = new Opportunity();
        Account acc = new Account();
        
        acc.Name = 'TestSurvey';
        acc.Survey_Code__c='HFReferal';
        acc.Survey_Dt__c = Date.today();
        acc.Survey_Opp_Id__c = opp.Id;
        System.debug('Bharath acc:' +acc);
        Insert acc;
        
        opp.Name = acc.Name;
        opp.AccountId = acc.Id;
        opp.StageName = 'Active';
        Date placedate = Date.newInstance(2011,03,01);
        opp.Placmnt_Start_Date_Arrive__c = placedate;
        opp.CloseDate = Date.today();
        //opp.Account = acc;
        //opp.Survey_Opp_Id_Match__c = 'Yes';
        //opp.Survey_Opp_Id_Match__c == 'No';
        System.debug('Bharath opp:' +opp);
        Insert opp;
        
        // List<Opportunity> o = [Select Name,AccountId,StageName,Placmnt_Start_Date_Arrive__c FROM Opportunity where Name = 'TestSurvey'];
        //List<Account> a = [Select Name,Survey_Code__c,Survey_Opp_Id__c,Survey_Dt__c  FROM Account where Name = 'TestSurvey'];
        
        HFSendSurveyClass sv = new HFSendSurveyClass();
        sv.SendSurvey(); 
     }
     
     //public static testMethod void testadddaystostringvalue()
     //{
     // string x = string.valueOf('Today+2');
     // HFSendSurveyClass s = new HFSendSurveyClass();
     // s.adddaystostringvalue(x); 
        //adddaystostringvalue(x);
     //}
     
     
}

 

 

bob_buzzardbob_buzzard

I'd suggest that you create a second test opportunity, where Survey_Opp_Match__c is a value other than 'No' and the account Survey_Code__c is 'HF1MoArv'.  This should allow you to hit the end section in red.

 

The other two sections can be hit by supplying a Survey_Date__c field that contains a '+' character.