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
anu112anu112 

Test class Code Coverage , i am getting 54% only

I was write  a test class, but i am getting code coverage 54% only. 

 

 

public class abhinav {


    private Apexpages.StandardController controller;

    Public string selectedPRC{get;set;}
    //Public callRedirect();

    String Id;
    public abhinav(ApexPages.StandardController controller) {
        Id = ApexPages.currentPage().getParameters().get('id');
    }


    //Our collection of the class/wrapper objects cContact
    public List<cContact> contactList{get;set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts(){
        
        
            contactList = new List<cContact>();
            for(Contact c: [select Id, firstName, lastname, Pcp_Roles__c from Contact where AccountId=:Id and Contact_Portal_ID__c!=null and Portal_User_Status__c='active'])
            {
                
                contactList.add(new cContact(c));
            }
       
        return contactList;
        
    }

    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        public cContact(Contact c) {
            con = c;
            //selected;    
        }
    }
    
    
    public List<SelectOption> getPRCRoles()
        {
                List<SelectOption> options = new List<SelectOption>();
                
               Schema.DescribeFieldResult fieldResult =
             Contact.Pcp_Roles__c.getDescribe();
             
               List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
                
               options.add(new SelectOption(  'none','-- Select One --'));
               for( Schema.PicklistEntry f : ple)
               {
                  options.add(new SelectOption( f.getValue(), f.getLabel()));
               }
            return options;
            //System.debug('Options --'+options);
        }    
        
        
    Public Pagereference save(){
                                            

                    // this.controller.save();
                    System.debug('What is selected PRC'+selectedPRC);
                    if(selectedPRC.equalsIgnoreCase('none'))
                    {
                        System.debug('I got here');
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a PCP role.'));
                        
                        return null;
                    }

            List<contact> selectedcontacts=new List<contact>();
           

            
                    for(cContact cCon: contactList) {
                    if(cCon.selected == true) {
                        selectedContacts.add(cCon.con);
                            }
                    }
                    System.debug('What is selected PRC and contact size-----'+selectedPRC);
                    System.debug('What is selected PRC and contact size-----'+selectedContacts.size());
                    if(selectedPRC != 'none' && selectedContacts.size() == 0)
                    {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Select atleast one contact'));
                        return null;
                    }
            
                    System.debug('These are the selected Contacts...'+selectedContacts);
                    for(Contact con: selectedContacts) {
                    system.debug(con);
                    
                        if(selectedPRC != 'none')
                        {
                        if(con.Pcp_Roles__c==null)
                                {
                                        con.Pcp_Roles__c=selectedPRC+';';
                                 }       
                        else {                                
                                    if(!con.Pcp_Roles__c.contains(selectedPRC))
                                    {  
                                        con.Pcp_Roles__c+=';'+selectedPRC;
                                    }
                               }
                               }      
                    }

            
            try{
                    update selectedcontacts;
                    callRedirect();
                    }catch(Exception e){
                          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Something happened: your action did not complete. Refresh the page and ry again.'));  
                    }
            
            return null;
            
                    
    }  
    
    Public Pagereference callRedirect(){
            System.debug('What is option '+selectedPRC);
            PageReference p = new PageReference('/' + Id);
            System.debug('What is completepagereferece '+p);
            p.setRedirect(true);
            return p;
                    
    }  

@istest
public static void test_abhinav()
    {
        Account a = new Account(id='0019000000LFNec');
        ApexPages.StandardController sc = new ApexPages.standardController(a);
       
        abhinav testcls = new abhinav(sc);
        List<cContact> aList = testcls.getContacts();
        testcls.ContactList=aList;
    
        List<SelectOption> aPRC = testcls.getPRCRoles();
      
        testcls.selectedPRC='none';
       // system.assertEquals(testcls.save(),null);
        for(SelectOption anOption : aPRC)
        {
            testcls.selectedPRC=String.valueOf(anOption);
            for(cContact con : testcls.ContactList)
            {
                con.selected=true;
            }
            system.assertEquals(testcls.save(),null);
        }

        
       // system.assertEquals(testcls.callRedirect(),null);
    }
}

 

 

<apex:page standardcontroller="Account" extensions="abhinav">
        
      <apex:form >
        <apex:pageMessages id="error"></apex:pageMessages>
        <apex:pageBlock title="GPRT Price Book Allocation" mode="edit" >
            <br></br>
            
            <apex:pageBlockButtons >
                 <apex:commandButton value="     Ok      " action="{!save}" rerender="Selected_PBS,error"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:commandButton value="     Cancel      " action="{!Cancel}"/>
            </apex:pageBlockButtons>
        

             
             <apex:outputText style="font-style:regular; font-weight:Regular; font-size:14px; font-family:Helvetica" value="Add PCP role" ></apex:outputText>
             <br></br>
                                                    
             <apex:selectList size="1" value="{!selectedPRC}" id="PRCoptions" style="width: 300px;"   Title="Add PCP Role">
             <apex:selectOptions value="{!PRCRoles}"/>
             </apex:selectList>
             <br></br><br></br><br></br>

                
                <apex:pageBlockTable value="{!contacts}" var="c" id="Selected_PBS">
                    <apex:column ><apex:inputCheckbox value="{!c.selected}" id="checkedone"/></apex:column>
                    <apex:column value="{!c.con.firstName}" />
                    <apex:column value="{!c.con.lastname}" />
                    <apex:column value="{!c.con.Pcp_roles__c}" headerValue="PCP Roles (Read Only)"/>
                </apex:pageBlockTable>
                
            </apex:pageBlock>
          
        </apex:form>
       
<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}   

function checkAll(cb,cbid)
        {
            var inputElem = document.getElementsByTagName("input");                     
            for(var i=0; i<inputElem.length; i++)
            {             
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                        
                    inputElem[i].checked = cb.checked;
                }
            }
        }
 
</script>
</apex:page>

Jeff MayJeff May

If you 'Run Tests' in Force IDE, the test results will list the lines that aren't covered.  By looking at those lines in your code, you can find the scenarios that you'll need to add to the tests.