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 

Test class code coverage to 75% please Help.

If i run my test class individually i am just able to see 22% as code coverage. And my original class does not have any code coverage. 

 

Class 

 

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); 
     List<Account> accList = new List<Account>(); 
                 
   for(Opportunity opp:oppList)
           
         {
            if(opp.AccountId!= null)
                {
                      Account acc = opp.Account; 
                      if(opp.Survey_Opp_Id_Match__c== 'No')
                          {
                            if(String.valueOf(config.SurveyDate__c) == 'Today')
                              {
                               hfsurvey = Date.Today();
                              }
                          else 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));
                              }
                              
                         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(acc.Survey_Code__c);
                         System.Debug(acc.Survey_Dt__c );
                         System.Debug(acc.Survey_Opp_Id__c );
                           }
                                     
                    else if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c))    
                        {                    
                            if(String.valueOf(config.SurveyDate__c) == 'Today')
                              {
                               hfsurvey = Date.Today();
                              }
                             else 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));
                              }
                             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(acc.Survey_Code__c);
                             System.Debug(acc.Survey_Dt__c );
                             System.Debug(acc.Survey_Opp_Id__c );
                       } 
                  
                 accList.add(acc);                
                }
             }
       update accList;                            
     }    
     
     private String adddaystostringvalue (string surveydays)
     {
        if(surveydays != null)
            surveydays = surveydays.substring(surveydays.indexOf('+')+ 1).trim(); 
            System.Debug(surveydays); 
            return surveydays;  
          
        
     }
}
Test class:
public class HFSendSurveyTestClass {
     static testMethod void SendSurvey()
     {
      Date hfsurvey;  
      // Get the most recently created config object.
         Config_Object__c config = new Config_Object__c();
         config.Name = 'HostFamily_Survey';
         config.HFSurveyCode__c = 'HF1MoArv';
         config.SurveyDate__c= 'Today+1';
         config.SOQL_query__c = string.ValueOf('Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__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');
         insert config;              
         
          Account acc = new Account();
          acc.Name = 'Testsurvey';
          acc.Survey_Code__c = 'HFReferal';
          acc.Survey_Dt__c= Date.today();                 
          insert acc;  
        
         Opportunity opp= new Opportunity();
         //opp.RecordType='HF FT Placemnt Rel 2';
         opp.Name='Testsurvey';
         opp.AccountId = acc.id; 
         opp.StageName='Active';       
         opp.Placmnt_Start_Date_Arrive__c = Date.newInstance(2011, 02, 14); 
         opp.CloseDate=Date.Today();   
         //opp.Account.Survey_Opp_Id__c = opp.id;   
         insert opp;    
         
         config = [Select HFSurveyCode__c,SurveyDate__c,SOQL_Query__C from Config_Object__C where Name ='HostFamily_Survey' limit 1];
         List<Opportunity> oppList = [Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__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];
         List<Account> accList = [Select Name,Survey_Code__c,Survey_Dt__c,Survey_Opp_Id__c from Account Where Name ='Testsurvey' And Survey_Code__c = 'HFReferal' limit 1];      
         
          for(Opportunity opps:oppList)  
         {
                if(opp.AccountId!= null)
                {
                      Account accs = opp.Account;                        
                      
          if(opp.Survey_Opp_Id_Match__c== 'No')
                  {
                    if(String.valueOf(config.SurveyDate__c) == 'Today')
                      {
                       hfsurvey = Date.Today();
                      }
               else if(String.valueOf(config.SurveyDate__c).contains('+'))
                      {
                        string s = String.valueOf(config.SurveyDate__c);
                        string intvalue = '1';
                        hfsurvey = Date.today().addDays(integer.valueOf(intvalue));
                      }
                         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);
                  }
                            
                                     
             else if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c))    
                   {
                    
                    if(String.valueOf(config.SurveyDate__c) == 'Today')
                      {
                       hfsurvey = Date.Today();
                      }
                 else if(String.valueOf(config.SurveyDate__c).contains('+'))
                      {
                        string s = String.valueOf(config.SurveyDate__c);
                        string intvalue = '1';
                        hfsurvey = Date.today().addDays(integer.valueOf(intvalue));
                      }
                         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(accs);
                 }
            }
         // update accList;  
        
        
        System.assertEquals(config.HFSurveyCode__c,acc.Survey_Code__c);
        System.assertEquals(hfsurvey,acc.Survey_Dt__c);
        System.assertEquals(opp.Id,acc.Survey_Opp_Id__c);
        System.Debug(acc.Survey_Code__c);
        System.Debug(acc.Survey_Dt__c);
        
     }
     
}

 

 

Can some one please susggest to increase my code coverage.

 

Thanks in Advance.

sfdcfoxsfdcfox

1) Are you getting any "failures" (i.e. SOQL query has no rows for assignment)? This will stop you cold in your tracks, which can reduce code coverage. Make sure your code does not suffer from test failures on the test results page.

 

2) It does not appear that you're testing both branches of your "if" statements. This means that I'd expect that 22% is approximtely correct, since you're only testing one of the two possible conditions for each statement. Instead, write a multitude of test methods, with each one testing the logic of one specific if branch. If you can't write the test method for a particular branch, consider if that branch is really necessary.

 

I hope that these two tidbits will help you reach the requisite 75% code coverage.

Brad007Brad007

Hi,

 

Thank you for your response. No i do have any test failures.

But my code still shows only 22 percent. i also removed one branch of if condtion.

 

I wanted to know if i am performing the unit test code correctly . please correct me here is the code.

 

public class HFSendSurveyTestClass {


     static testMethod void SendSurvey()
     {
      Date hfsurvey;  
      // Get the most recently created config object.
         Config_Object__c config = new Config_Object__c();
         config.Name = 'HostFamily_Survey';
         config.HFSurveyCode__c = 'HF1MoArv';
         config.SurveyDate__c= 'Today';
         config.SOQL_query__c = string.ValueOf('Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__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');
         insert config;              
         
          Account acc = new Account();
          acc.Name = 'Testsurvey';
          acc.Survey_Code__c = 'HFReferal';
          acc.Survey_Dt__c= Date.today();                 
          insert acc;  
        
         Opportunity opp= new Opportunity();
         //opp.RecordType='HF FT Placemnt Rel 2';
         opp.Name='Testsurvey';
         opp.AccountId = acc.id;
         opp.StageName='Active';       
         opp.Placmnt_Start_Date_Arrive__c = Date.newInstance(2011, 02, 14);
         opp.CloseDate=Date.Today();   
         //opp.Account.Survey_Opp_Id__c = opp.id;   
         insert opp;    
         
         config = [Select HFSurveyCode__c,SurveyDate__c,SOQL_Query__C from Config_Object__C where Name ='HostFamily_Survey' limit 1];
         List<Opportunity> oppList = [Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__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];
         List<Account> accList = [Select Name,Survey_Code__c,Survey_Dt__c,Survey_Opp_Id__c from Account Where Name ='Testsurvey' And Survey_Code__c = 'HFReferal' limit 1];      
         
          for(Opportunity opps:oppList)  
         {
                if(opp.AccountId == acc.id)
                {
                      Account accs = opp.Account;  
                                     
              if(String.valueOf(config.HFSurveyCode__c) != 'HF1MoArv')    
                   {
                    
                    if(String.valueOf(config.SurveyDate__c) == 'Today')
                      {
                       hfsurvey = Date.Today();
                       System.assertequals(hfsurvey,Date.Today());
                      }
                 else if(String.valueOf(config.SurveyDate__c).contains('+'))
                      {
                        string s = 'HF1MoArv';
                        string intvalue = '';
                        hfsurvey = Date.today().addDays(integer.valueOf(intvalue));
                      }
                         string oppcode= opp.Id;
                         acc.Survey_Code__c = 'HF1MoArv';
                         acc.Survey_Dt__c = hfsurvey;
                         acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                         System.assertEquals('HF1MoArv',acc.Survey_Code__c);
                         System.assertEquals(hfsurvey,acc.Survey_Dt__c);
                         System.assertEquals(opp.Id,acc.Survey_Opp_Id__c);
                         System.Debug('Surveycode:' +acc.Survey_Code__c);
                         System.Debug('OppID:' +acc.Survey_Opp_Id__c);
                         System.Debug('Surveydate:' +acc.Survey_Dt__c);
                   }
                 //accList.add(accs);
                 }
            }
         // update accList;  
        
        
               
        //System.Debug(acc.Survey_Dt__c);
        
     }
  }