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
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan 

problem in code coverage

 
public with sharing class empdepExtensionNew
{

    public Contact[] Availabledeps {get;set;}
    public dep__c[] shoppingCart{get; set;}
    //public dep__c[] total{get; set;}
    public emp__c theemp {get; set;}
    public String tonSelect{get; set;}
    public String toUnselect{get; set;}
    public String searchString{get; set;}
    //public String value{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 dep__c[] forDeletion = new dep__c[]{};
    private ApexPages.StandardController controller;

    //Constructor

    public empdepExtensionNew (ApexPages.StandardController controller)
    {
        this.controller= controller;
        if(controller.getrecord() == null)
        system.debug(controller.getrecord().id);

        //UserID = UserInfo.getname(); 

        //emping Custom Setting object for Recordtypes in emp 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 emp 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;
        }

      { 
            //String value = ApexPages.currentPage().getParameters().get('Id');

            //total =[Select id,name,empId__c,Contact_Id__c,emp_Date__c,dep__c.Contact_Id__r.Name from dep__c ];
            shoppingCart = [Select id,name,empId__c,Contact_Id__c,emp_Date__c,dep__c.Contact_Id__r.Name from dep__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(dep__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 );               
        Availabledeps = database.query(qString);
        system.debug(Availabledeps);

    } 

    public void addToShoppingCart()

    // This function runs when a user hits "select" button next to a dep

    { 
      for(Contact part : Availabledeps)
       {
        if((String)part.id==tonSelect)
            {

                shoppingCart.add(new dep__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 dep" section

        Integer count = 0;

        for(dep__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 (dep__c partmember : shoppingCart ){
                     partmember.empId__c=controller.getrecord().id;
                     System.debug(partmember.empId__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 emp
       return new PageReference('/' + controller.getrecord().id);  
    }     
}
Test class:
@istest(seealldata=true)
Public class Test_empdepExtensionNew
{
Public static testmethod void empdepExtensionNew_test()
{
    Record_Type_Name__c opprt =[select  ID,name,Record_Type__c from Record_Type_Name__c where name ='OppRecordTypeID']; 
    PageReference nextpage = new PageReference('/apex/empCustomNew?id='+opprt.Id);
    nextpage.setredirect(true);
    emp__c  c = new emp__c(Subject__c = 'TestdepExtension',emp_Type__c = 'BD Monthly Update',emp_Date__c = system.today(),
    Status__c = 'DONE',OpportunityId__c='006q0000004QWR7',RecordtypeId=opprt.Record_Type__c);
    insert c;
    empdepExtensionNew e = new  empdepExtensionNew(new ApexPages.StandardController(c));
    contact con = new contact(LastName='test',Contact_Type__c='Business contact',AccountId='001q000000BDufL');
    insert con;
        e.updateAvailableList();
        e.addToShoppingCart();
        e.removeFromShoppingCart();
        e.onSave(); 
        dep__c p1 = new dep__c(empId__c=c.id,Contact_Id__c='003q000000CrgjD');
        dep__c p2 = new dep__c(empId__c=c.id,Contact_Id__c='003q000000CvPct');
        List<dep__c> part = new List<dep__c>();
        part.add(p1);
        part.add(p2);
        insert part; 
        //System.assertEquals(3, [select count() from dep__c where empId__c=:c.id]);
    }    
}

At present code coverage is 66%.Kindly help me out to increae my code coverage. thanks
Waqar Hussain SFWaqar Hussain SF
Which part of your code is not covering in test class, can you explain please?
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Hi Vickey,

1. In the method updateAvailableList , we are getting the search string via the properties and querying for the records
    How to implement this in test class as we didnt pass any parameter to that method ?

2.  how to write test class for custom settings listed in the constructor method empdepExtensionNew ?

 
Waqar Hussain SFWaqar Hussain SF
@istest(seealldata=true)
Public class Test_empdepExtensionNew
{
Public static testmethod void empdepExtensionNew_test()
{
    Record_Type_Name__c opprt =[select  ID,name,Record_Type__c from Record_Type_Name__c where name ='OppRecordTypeID']; 
    PageReference nextpage = new PageReference('/apex/empCustomNew?id='+opprt.Id);
    nextpage.setredirect(true);
    emp__c  c = new emp__c(Subject__c = 'TestdepExtension',emp_Type__c = 'BD Monthly Update',emp_Date__c = system.today(),
    Status__c = 'DONE',OpportunityId__c='006q0000004QWR7',RecordtypeId=opprt.Record_Type__c);
    insert c;
    empdepExtensionNew e = new  empdepExtensionNew(new ApexPages.StandardController(c));
    contact con = new contact(LastName='test',Contact_Type__c='Business contact',AccountId='001q000000BDufL');
    insert con;
		Id [] fixedSearchResults= new Id[1];
        fixedSearchResults[1] = con.Id;
        //Required so SOSL will fire in Unit Tests Properly
        Test.setFixedSearchResults(fixedSearchResults);
        
        e.searchString = 'test';
        
        test.startTest();
        rs.search();
        
        e.updateAvailableList();
        e.addToShoppingCart();
        e.removeFromShoppingCart();
        e.onSave(); 
        test.stopTest();
        
		dep__c p1 = new dep__c(empId__c=c.id,Contact_Id__c='003q000000CrgjD');
        dep__c p2 = new dep__c(empId__c=c.id,Contact_Id__c='003q000000CvPct');
        List<dep__c> part = new List<dep__c>();
        part.add(p1);
        part.add(p2);
        insert part; 
        //System.assertEquals(3, [select count() from dep__c where empId__c=:c.id]);
    }    
}
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Variable doesnot exists rs
Swayam  AroraSwayam Arora
Hi Vidhyasagaran,

The test class you have written is totally wrong. Hardcoded Ids should not be used. Migration to other orgs will be failed.

Regards,
Swayam Arora