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
salesforcemicky125salesforcemicky125 

Unable to cover the code coverage for a Class by writing a test class

Hi ,

I was assigned to write a  Test class , where i was unable to cover the code coverage for the entire class i can able to pass the test class, but unable to cover the code coverage

Here is my Test class

@isTest(SeeAllData=true)
public class Test_CreateContactController
{

private static testMethod void TestCreateContactController()
  {
        
          Set<Id> sObjectIds = new Set<Id>(); 
          Set<id> sObjectrecIds = new Set<id>();
         set<id> campusrecid = new set<id>();
          List<id> acids = new List<id>();         
           List<RecordType> RecordTypeids = new List<RecordType>();        
         contact con=new contact();
         con.Lastname='Vishalg';
         con.Email='gaur@deloitte.com';
         con.FirstName='Gaur';
         con.Title='SiteDemo';
         con.Phone='7875434250';
        //con.recordtypeid=RecordTypeids[0].id;       
       // con.Recruiting_Event__c=objRecEvent.ID;    
        
         insert con;
        
           List<Event_Registration__c> ObjEvent = [Select id,Name,Contact__c,Recruiting_Event__c,Attended__c,Attending__c  from Event_Registration__c where id =:sObjectIds order by id limit 10]; 
            if (ObjEvent.size() > 0)
            {
           
             ObjEvent[0].Attended__C = true;
            ObjEvent[0].Attending__C = true;
             update ObjEvent;
            } 
           
            
              Recruiting_Event__c objRecEvent =  new Recruiting_Event__c();// [SELECT id,name,Location__c,Season__c,Account__c,Room__c,Status__c,Start_Date__c,End_Date__c FROM Recruiting_Event__c  limit 1];
               objRecEvent.name='Madhava Palla Event';
             objRecEvent.Season__c='Winter 2014';
              objRecEvent.Account__c='001R000000oYU5K';
              objRecEvent.Start_Date__c=system.today()-1;        
              objRecEvent.End_Date__c=system.today()+1;
               insert objRecEvent; 
             
               //List<Account> school = new List<Account>(); 
               //school=[Select id,Name from Account where id in :acids order by Name];  
              //insert school;
             
            List<Account> school = new List<Account>();
            //school.Name='001R000000oYU5K';
            //insert school;
                 


      ApexPages.StandardController sc = new ApexPages.StandardController(con);   
       CreateContactController createCon = new CreateContactController();  
           
       PageReference pageRef = Page.newregistration;
       Test.setCurrentPage(pageRef);
     
            
     createCon.displaySearchResults = true;
     createCon.selectedValue = '';
     createCon.schoolName ='';
     createCon.emailc ='';
     createCon.closePopup();
     createCon.getDisplaypblock();
    createCon.getschoollist();
    createCon.showPopup();
    createCon.cancel();
    createCon.getschoollist();     
    createCon.incrementCounter();
    createCon.displayPopUp = true;
      createCon.displaySearchResults = false;
     
     }

}

Here is the Class that i was to write the Test Class

public class CreateContactController
{
   public PageReference closePopup() {
        displayPopup = false;
        return null;
    }
    public boolean displayPopUp { get; set; }
    public PageReference showPopup() {
        displayPopup = true;
        return null;
    }
    public String getDisplaypblock() {
        return null;
    }
    public string schoolname{get;set;}
    public string emailc{get;set;}
    public Contact con{get; set;}
    public List<Contact> objContact {get;set;}
    public Event_Registration__c ObjEvent{get;set;}
    public List<Recruiting_Event__c> objRecEvent{get;set;}
    public Recruiting_Event__c ObjAccountRec{get;set;}
    public Boolean displaySearchResults{get;set;}
    public Recruiting_Event__c newrecevent{get;set;}
    public String selectedValue {get
    {
        if(selectedValue <> null && selectedValue <> '')
        {
            return selectedValue ;
        }
        else{
            selectedValue = '';
            return selectedValue ;
        }
    } set;}
    public List<id> acids = new List<id>();
    public List<Recruiting_Event__c> recevents= new List<Recruiting_Event__c>();
    public List<Account> school = new List<Account>();
    public List<Contact> user=new List<Contact>();
    public CreateContactController(ApexPages.StandardController controller) {
    newrecevent = new Recruiting_Event__c();
    this.newrecevent =(Recruiting_Event__c)controller.getRecord();
    con = new contact();
    }
    public PageReference Cancel() {
        return null;
    }
public String getschoollist() {
        return schoolname;
    }
    public CreateContactController()
    {
        con=new Contact ();
        ObjAccountRec = new Recruiting_Event__c();      

}
   public List<SelectOption> schoollist
    {
        get
        {   recevents= [Select Account__c,Name From Recruiting_Event__c];
            schoollist = new List<SelectOption>();
         for(Recruiting_Event__c temp : recevents)
            {  

                acids.add(temp.Account__c);
            }
         school=[Select id,Name from Account where id in :acids order by Name];  
         schoollist.add(new selectOption('--None--','--None--'));
         for(Account temp1: school)
         {
        schoollist.add(new SelectOption(temp1.Id,temp1.Name));
         }  
         system.debug('shcooltypename1'+schoolname);
        return schoollist;
                }
        set;
    }
public PageReference incrementCounter() {
        schoolname=getschoollist();
        displaySearchResults=true;
        date Next = system.today()+2;
        date cnow = system.today();
        date Last = system.today()-2;
        system.debug('systemNext'+Next);
        system.debug('systemLast'+Last);
        objContact=[SELECT id,name,email from contact where email=:con.Email limit 2];
        objRecEvent=[SELECT id,name,Location__c,Season__c,Account__c,Room__c,Status__c,Start_Date__c,End_Date__c FROM Recruiting_Event__c  WHERE Account__c =:selectedValue and Start_Date__c >=:Last and Start_Date__c <=:Next and Status__c<>'Completed'];     
        set<id> recids = new set<id>();
        set<id> campusrecid = new set<id>();
        List<RecordType> RecordTypeids = new List<RecordType>();
        RecordTypeids =[select id,name from RecordType where sobjecttype = 'Contact' and name='campus' limit 1];
        con.recordtypeid=RecordTypeids[0].id;
        con.Accountid=objRecEvent[0].Account__c;
        if(ObjContact.size()<=0){
        insert con;
        PageReference cr=new PageReference('https://eevent-deloittenet.cs2.force.com/event/newregistration_rec?cid='+con.id+'&acid='+objRecEvent[0].Account__c);
        cr.setRedirect(true);
        return cr;
        }      
        else
        {
        PageReference dr=new PageReference('https://eevent-deloittenet.cs2.force.com/event/newregistration_rec?cid='+objContact[0].id+'&acid='+objRecEvent[0].Account__c);
        dr.setRedirect(true);
        return dr;       
        }
       
       
    }
   
}
Pradeep RajuPradeep Raju
Hi,

You mean you are not able to cover entire class ???
salesforcemicky125salesforcemicky125
Hi Pradeep, I have attached the screen shot where we can see the line with red color are Not covered for the code coverage. Can you help me out on how to call the Standard controller . So that i can achieve the code coverage min 75% right now it is showing me 46% . It would be great help for me. I am also getting error below error when i run test class when i include the method " Increment Counter " if i have cover the this method it will cover code coverage upto 70% but it is failing Error MessageSystem.ListException: List index out of bounds: 0Stack TraceClass.CreateContactController.incrementCounter: line 92, column 1 Class.Test_CreateContactController.TestCreateContactController: line 62, column 1 [image: Inline image 2]
Abhi_TripathiAbhi_Tripathi
Hey Pradeep,

take a look at this post, surely it'll help you

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi
Salesforce Developer