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
Dhanesh SubramanianDhanesh Subramanian 

test coverage for my code is 59%.Retail and program are master and detail obj. for a retail rec multiple program gets added. user have option to select program by searching for program

My Controller code: 
public with sharing class retailprogramExtensionNew
{
    
    public Contact[] Availableprograms {get;set;}
    public program__c[] shoppingCart{get; set;}
    public retail__c theretail {get; set;}
    public String tonSelect{get; set;}
    public String toUnselect{get; set;}
    public String searchString{get; set;}
    public Boolean AccountRT{get; set;}
    public Boolean ContactRT{get; set;}
    public Boolean LeadRT{get; set;}
    public Boolean OpportunityRT{get; set;}
    public String UserID {get; set;}
    
    private program__c[] forDeletion = new program__c[]{};
    private ApexPages.StandardController controller;
    
    //Constructor
    
    public retailprogramExtensionNew (ApexPages.StandardController controller)
    {
        this.controller= controller;
        if(controller.getrecord() == null)
        system.debug(controller.getrecord().id);
        
        //UserID = UserInfo.getname(); 
        
        //retailing Custom Setting object for Recordtypes in retail object
        
        Record_Type_Name__c Accrt = Record_Type_Name__c.getValues('AccRecordTypeID');
        Record_Type_Name__c Conrt = Record_Type_Name__c.getValues('ConRecordTypeID');
        Record_Type_Name__c Leart = Record_Type_Name__c.getValues('LeaRecordTypeID');
        Record_Type_Name__c Opprt = Record_Type_Name__c.getValues('OppRecordTypeID');
        
        //Condition to check the record type for retail object 
        
        if(ApexPages.currentPage().getParameters().get('RecordType') == Accrt.Record_Type__c){
            AccountRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Conrt.Record_Type__c){
            ContactRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Leart.Record_Type__c){
            LeadRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Opprt.Record_Type__c){
            OpportunityRT = TRUE;
        }
       
      { 
            
            shoppingCart = [Select id,name,retailId__c,Contact_Id__c,retail_Date__c,program__c.Contact_Id__r.Name from program__c where id =: tonSelect]; 
            
       }
       updateAvailableList();
     }
      
    public void updateAvailableList() 
    {
      
        UserID = UserInfo.getUserId(); 
        String qString =  'select id, Name, Title, Contact.MailingCity,Contact.MailingState,Contact.Account.Name from Contact  where User_Id__c not in (select ID from User where id =: UserID )' ;
        system.debug(qString);
        
        if(searchString!=null)
        
        {          
            qString+= ' and ( Contact.Name like \'%' + searchString + '%\' or Contact.RACFID__c like \'%' + searchString + '%\' or Contact.Officer_Code__c like \'%' + searchString + '%\') ';                       
        }
        
       Set<Id> selectedEntries = new Set<Id>();
       if(tonSelect!=null)
        for(program__c d : shoppingCart){
            selectedEntries.add(d.Contact_Id__c);
        }
        
        if(selectedEntries.size()>0){
            String tempFilter = ' and id not in (';
            for(id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
            
            qString+= extraFilter;
        } 
        
        qString+= ' order by Name';
        qString+= ' limit 12';
        system.debug('qString:' +qString );               
        Availableprograms = database.query(qString);
        system.debug(Availableprograms);
        
    } 
    
    public void addToShoppingCart()
    
    // This function runs when a user hits "select" button next to a program
    
    { 
      for(Contact part : Availableprograms)
       {
        if((String)part.id==tonSelect)
            {
                 
                shoppingCart.add(new program__c (Contact_Id__c =part.id));
                system.debug(shoppingCart);
                system.debug(shoppingCart.size());
                break;
                
            }          
              
        }
         updateAvailableList();
             
    }  
    
      public PageReference removeFromShoppingCart(){
    
        // This function runs when a user hits "remove" on "Selected program" section
    
        Integer count = 0;
    
        for(program__c del : shoppingCart){
            if((String)del.Contact_Id__c==toUnselect){
            
                if(del.Id!=null)
                    forDeletion.add(del);
            
                shoppingCart.remove(count);
                break;
            }
            count++;
        }
        
        updateAvailableList();
        
        return null;
    }
    
     // This function runs when user hits save button
     
     public PageReference onSave(){
             
        try{
            
            PageReference pageRef = controller.save();
            system.debug(controller.getrecord().id);
                        
                if(shoppingCart.size()>0) 
                  
                  for (program__c partmember : shoppingCart ){
                     partmember.retailId__c=controller.getrecord().id;
                     System.debug(partmember.retailId__c);
                  }
                  
                  System.debug('size' +shoppingCart.size());
                  insert(shoppingCart);
                }
           
        
            catch(Exception e){
            ApexPages.addMessages(e);
            return null;
        }  
           System.debug('completed');

        // After save return the user to the retail
       return new PageReference('/' + controller.getrecord().id);  
    }     
}

My Test class
 
@istest(seealldata=true)
Public class Test_retailprogramExtensionNew
{
    Public static testmethod void retailprogramExtensionNew_test()
    {
        Record_Type_Name__c opprt =[select  ID,name,Record_Type__c from Record_Type_Name__c where name ='OppRecordTypeID']; 
        
        Opportunity opp1 = new Opportunity(name='test1', Product_Category__c='ATM', Product__c='ATM',Private__c=true, Private_Code_Name__c='test1',CloseDate=system.today(), StageName='Pursue');
        insert opp1;
        
        contact con = new contact(LastName='Mr. Dhanesh Subramanian',RACFID__c='HINDF', Officer_Code__c='HINDF',Contact_Type__c='Key Employee',Salesforce_User__c=true, );
        insert con;
        
        contact con1 = new contact(LastName='Vidhyasagaran Muralidharan',RACFID__c='MURALVI',Officer_Code__c='HINDF1',Contact_Type__c='Key Employee',Salesforce_User__c=true );
        insert con1;
         
        Test.setCurrentPageReference(new PageReference('Page.retailCustomNew'));
        System.currentPageReference().getParameters().put('id',opprt.id);
        
        retailprogramExtensionNew e = new  retailprogramExtensionNew(new ApexPages.StandardController(opprt));
        
        retail__c  retail = new retail__c();
        retail.Subject__c = 'Test';
        retail.retail_Type__c = 'BD Monthly Update';
        retail.retail_Date__c = system.today();
        retail.Is_Private__c = TRUE;
        retail.Status__c = 'DONE';
        retail.Comments__c = 'sample test for test class';
        retail.recordtypeId = opprt.id;
        insert retail; 
          
        e.searchString ='Mr. Dhanesh Subramanian';
        e.searchString ='SUBRAD2';
        e.searchString ='HINDF';
        e.searchstring ='';
        e.updateAvailableList();  
        e.addToShoppingCart();
        e.removeFromShoppingCart();
        e.onSave();        
        
        test.startTest();
        program__c  part = new program__c  ();
        part.Contact_Id__c = con.id;
        part.retailId__c = retail.id; 
        insert part; 
        test.stopTest();   
    }    
}

 
pconpcon
You will need to add additional tests to cover your other parts.  I recommend that you split your tests into several covering each of your methods individually.  For example one that calls updateAvailableList with a query and one that calls it without.  You'll need to repeat this to cover all of the if statements in your code.

Additionally, none of your controller code is actually being tested in your test above.  This is because all of you retailprogramExtensionNew method calls are outside or your start/stop test.

Also I would highly recommend that you do not use seealldata unless you have to (and you don't have to for this test) but instead create all the data inside of your test method.  And finally, you have no asserts in your test at all, so you are not actually verifying that your code does as it says.  While you may reach the required 75% coverage needed to deploy, you are not actually testing your code.