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
Long TruongLong Truong 

Increase Code Coverage

I have this wrapper class with 68% code coverage. Below is the class and the test class. Could some one please help me increase the code coverage? Thanks!
public with sharing class CRequestForm_Wrapper {
    
    public List<ContactReq> wrapperList {get;set;}
    public Contact cContact=new Contact();
    public Contact_Request_Form__c cRForm=new Contact_Request_Form__c();
    public Contact_Request_Form__c cRFormNotes {get;set;}
    public boolean errormsg=false;
    public boolean ApprovalFormShowChangeDetails {get;set;}
    public String errorDetail='';
    
    public Boolean isPartnerSubgrantee {get;set;}
    public Boolean noRoleFound {get;set;}
    public List<SelectOption> subgranteeRolesList {get;set;}
    public List<SelectOption> partnerProfilesList {get;set;}
    

    public CRequestForm_Wrapper(ApexPages.StandardController controller)
    {
      String id=ApexPages.currentPage().getParameters().get('cid');
      if(id!=null)
      {
          cContact=[select Id,LastName,FirstName,MI__c,Fax,MobilePhone,HomePhone,OtherPhone,Email,Title,Department,Contact_Mailing_Street_1__c ,Contact_Mailing_Street_2__c
                            ,Contact_Mailing_City__c ,Contact_Mailing_Zip_Code__c ,SSAI_Sponsors__c,SSAI_Directors__c,
                            SSAI_Fiscals__c,D2D_Contacts__c,Contact_Shipping_Street_1__c, SSAI_SCSEP_Role__c
                            ,Contact_Shipping_Street_2__c ,Contact_Shipping_City__c ,Contact_Shipping_Zip_Code__c ,
                            Contact_Fax__c , Contact_Mailing_State__c ,Contact_Shipping_State__c,Shipping_Organization__c,Mailing_Organization__c,Shipping_Department__c,AccountId from Contact where id =:id limit 1];
                          wrapperList=new List<ContactReq>();
                            
          wrapperList.add(new ContactReq( NewCRequest(cContact), cContact));
          
      }
      String cRid=ApexPages.currentPage().getParameters().get('id');
      if(cRid!=null)
      {
        cRForm=[select Id, Subgrantee_Name__c, Subgrantee_Name__r.IsPartner,Last_Name__c,First_Name__c,MI__c, SSAI_SCSEP_Role__c,
                    Fax_Phone__c, Mobile_Phone__c, Home_Phone__c, Business_Phone__c,
                    Email_Address__c, Job_Title__c, Department_B__c, Department_S__c,
                    Organization_S__c, Organization_B__c,
                    Address_Line_1B__c, Address_Line_2B__c, City_B__c, Zip_Code_B__c, Business_State__c,
                    SSAI_Fiscal__c, SSAI_Spon__c, SSAI_Directors__c, D2D_Contacts__c,
                    Address_Line_1S__c, Address_Line_2S__c, City_S__c, Zip_Code_S__c, Shipping_State__c,
                    Notes__c, Is_Create_User__c, Role__c, Profile__c, Contact__c, Forms_and_Information__c,Pay_by_Pay__c,Report_of_Costs_SA1_and_SA2__c,Salesforce_Portal_Account__c,SCSEP_Eligibility_Determination__c, SPARQ__c,Is_Delete_Request__c from Contact_Request_Form__c where id =:cRid limit 1];
        cRFormNotes=cRForm;
    
        if(CRFormNotes.Subgrantee_Name__c!=null && CRFormNotes.Contact__c==null)
        {
            ApprovalFormShowChangeDetails = false;
            isPartnerSubgrantee = CRFormNotes.Subgrantee_Name__r.IsPartner;
            noRoleFound = false;
            subgranteeRolesList = getPartnerRoles(CRFormNotes.Subgrantee_Name__c);
            partnerProfilesList = getPartnerProfiles();
        }
        else{
            ApprovalFormShowChangeDetails = true;
            cContact=[select Id,LastName,FirstName,MI__c,Fax,MobilePhone,HomePhone,OtherPhone,Email,Title, SSAI_SCSEP_Role__c,Department,Contact_Mailing_Street_1__c ,Contact_Mailing_Street_2__c
                        ,Contact_Mailing_City__c ,Contact_Mailing_Zip_Code__c ,SSAI_Sponsors__c,SSAI_Directors__c,
                        SSAI_Fiscals__c,D2D_Contacts__c,Contact_Shipping_Street_1__c 
                        ,Contact_Shipping_Street_2__c ,Contact_Shipping_City__c ,Contact_Shipping_Zip_Code__c ,
                        Contact_Fax__c , Contact_Mailing_State__c ,Contact_Shipping_State__c,Shipping_Organization__c,Mailing_Organization__c,Shipping_Department__c,AccountId from Contact where id =:CRFormNotes.Contact__c limit 1];
            wrapperList=new List<ContactReq>();
                        
            wrapperList.add(new ContactReq( cRFormNotes, cContact));
        }
      }
                                       
    }
    
    public Contact_Request_Form__c NewCRequest(Contact cContact)
    {
        
        System.debug('Inside shree');
        Contact_Request_Form__c  c  = new Contact_Request_Form__c();
            c.Subgrantee_Name__c    =cContact.AccountId;                    
            c.Last_Name__c          =cContact.LastName ;
            c.First_Name__c         =cContact.FirstName ;
            c.MI__c                 =cContact.MI__c ;
            
            c.Fax_Phone__c          =cContact.Fax ;
            c.Mobile_Phone__c       =cContact.MobilePhone; 
            c.Home_Phone__c         =cContact.HomePhone ;
            c.Business_Phone__c     =cContact.OtherPhone ;
            
            c.Email_Address__c      =cContact.Email ;
            c.Job_Title__c          =cContact.Title; 
            c.SSAI_SCSEP_Role__c    =cContact.SSAI_SCSEP_Role__c;
            c.Department_B__c       =cContact.Department;
            c.Department_S__c       =cContact.Shipping_Department__c;
            
            c.Organization_S__c     =cContact.Shipping_Organization__c;
            c.Organization_B__c     =cContact.Mailing_Organization__c;
                    
            
            c.Address_Line_1B__c    =cContact.Contact_Mailing_Street_1__c;  
            c.Address_Line_2B__c    =cContact.Contact_Mailing_Street_2__c ; 
            c.City_B__c             =cContact.Contact_Mailing_City__c ; 
            c.Zip_Code_B__c         =cContact.Contact_Mailing_Zip_Code__c ; 
            c.Business_State__c     =cContact.Contact_Mailing_State__c  ;
            
            c.SSAI_Fiscal__c        =cContact.SSAI_Fiscals__c  ;
            c.SSAI_Spon__c          =cContact.SSAI_Sponsors__c  ;
            c.SSAI_Directors__c     =cContact.SSAI_Directors__c  ;
            c.D2D_Contacts__c       =cContact.D2D_Contacts__c  ;
            
            c.Address_Line_1S__c    =cContact.Contact_Shipping_Street_1__c;  
            c.Address_Line_2S__c    =cContact.Contact_Shipping_Street_2__c ; 
            c.City_S__c             =cContact.Contact_Shipping_City__c  ;
            c.Zip_Code_S__c         =cContact.Contact_Shipping_Zip_Code__c;  
            c.Shipping_State__c     =cContact.Contact_Shipping_State__c  ;
            
            //c.Fax_Phone__c        =cContact.Contact_Fax__c  ;
            c.Fax_Phone__c          =cContact.Fax  ;
            c.Contact__c            =cContact.Id;
            c.Notes__c              ='';
            
            return c;
        
        
    }
    public boolean geterrormsg()
    { 
        return errormsg;
    }
    public String geterrorDetail()
    { 
        return errorDetail;
    }

    public class ContactReq{
        
        public Contact_Request_Form__c CReq{get;set;}
        public Contact c{get;set;}

         public ContactReq(Contact_Request_Form__c CReq , Contact c)
        {
            this.CReq = CReq;
            this.c = c;
        }

    
    }
    
    public Pagereference Save()
    {        
        Pagereference p =new Pagereference('/a02/o');
        Contact_Request_Form__c  NewContactRequest= wrapperList[0].CReq;
        NewContactRequest.Status__c='New';
        List <String> checkStatus=new List<string>() ;
        checkStatus.add('New');
        checkStatus.add('Submitted');
        List<Contact_Request_Form__c> ExistingCRform=[select Id,Subgrantee_Name__c,Email_Address__c,Status__c from Contact_Request_Form__c where 
                                                        Email_Address__c =:NewContactRequest.Email_Address__c 
                                                        and Subgrantee_Name__c =:NewContactRequest.Subgrantee_Name__c 
                                                        and Status__c IN :checkStatus limit 1];
        if(ExistingCRform.size()!=0)
        {
            errorDetail='Contact Request Form with status [ '+ExistingCRform[0].Status__c +']already exists for the email id ['+ExistingCRform[0].Email_Address__c+']and Sub-Garentee['+ExistingCRform[0].Subgrantee_Name__c+']';
            errormsg=true;     
            return null;
        }
        else
        {
            insert NewContactRequest;
            return p;
        }
    } 
    
    public Pagereference Submit()
    {        
        Pagereference p =new Pagereference('/a02/o');
        Contact_Request_Form__c  NewContact= wrapperList[0].CReq;
        NewContact.Status__c='Submitted';
        upsert NewContact;
        return p;
    } 
    public Pagereference onApprove()
    {        
        if(validate())
        {
            Id CRequestId=ApexPages.currentPage().getParameters().get('id');
            Pagereference p =new Pagereference('/'+CRequestId);
            Contact_Request_Form__c  editCrForm= new Contact_Request_Form__c(Id=CRequestId);
            editCrForm.Status__c='Approved';
            editCrForm.Notes__c=cRForm.Notes__c;
            if(CRForm.IS_Create_User__c && CRFormNotes.Contact__c==null)
            {
                editCRForm.IS_Create_User__c = CRForm.IS_Create_User__c;
                editCRForm.Role__c = CRForm.Role__c;
                editCRForm.Profile__c = CRForm.Profile__c;
            }
            List<RecordType> recordTypeVals= [select Id from RecordType where Name = 'Form Approver' limit 1];
            editCrForm.RecordTypeId=recordTypeVals[0].id;
            upsert editCrForm;
            return p;
        }
        else
        {
            return null;
        }
    } 
    public Pagereference onReject()
    {        
        Id CRequestId=ApexPages.currentPage().getParameters().get('id');
        Pagereference p =new Pagereference('/'+CRequestId);
        Contact_Request_Form__c  editCrForm= new Contact_Request_Form__c(Id=CRequestId);
        editCrForm.Status__c='Rejected';
        editCrForm.Notes__c=cRForm.Notes__c;
        List<RecordType> recordTypeVals= [select Id from RecordType where Name = 'Form Approver' limit 1];
        editCrForm.RecordTypeId=recordTypeVals[0].id;
        upsert editCrForm;
        return p;
    }
    public Pagereference onSubmit()
    {        
        Id CRequestId=ApexPages.currentPage().getParameters().get('id');
        Pagereference p =new Pagereference('/'+CRequestId);
        Contact_Request_Form__c  editCrForm= new Contact_Request_Form__c(Id=CRequestId);
        editCrForm.Status__c='Submitted';
        upsert editCrForm;
        return p;
    } 
    
    public List<selectOption> getPartnerRoles(Id accId)
    {
        List<UserRole> urList = new List<UserRole>();
        List<SelectOption> rolesList = new List<SelectOption>();
        rolesList.add(new SelectOption('','--None--'));
        
        if(isPartnerSubgrantee)
        {
            urList = [Select Id, Name from UserRole where PortalType = 'Partner' and PortalAccountId = :accId];
            if(urList.size()>0)
            {
                for(UserRole ur : urList)
                {
                    rolesList.add(new SelectOption(ur.Id,ur.Name));
                }
            }
            else
            {
                noRoleFound = true;
                //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'No Role found. The user will be created with default Role'));
            }
        }
        /*else
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Role found. Please enable the Subgrantee for Partner Portal'));
        }*/

            return rolesList;
    } 
    
    public List<selectOption> getPartnerProfiles()
    {
        List<Profile> prList = new List<Profile>();
        prList = [Select Id, Name from Profile where UserType = 'PowerPartner' and Name != 'Gold Partner User' order by Name];
        
        List<SelectOption> profilesList = new List<SelectOption>();
        profilesList.add(new SelectOption('','--None--'));
        
        if(prList.size()>0)
        {
            for(Profile pr : prList)
            {
                profilesList.add(new SelectOption(pr.Id,pr.Name));
            }
            return profilesList;
        }
        else
        {
            System.debug('No Partner Profile found');
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Partner Profile found'));
            return null;
        }
    } 
    
    public Boolean validate()
    {
        System.debug('*****'+CRForm.Is_Create_User__c+'*****'+CRForm.Profile__c+'*****'+CRForm.Role__c);
        if(CRForm.Is_Create_User__c)
        {
            if(noRoleFound && CRForm.Profile__c==null)
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please choose a profile'));
                return false;
            }
            else if(!noRoleFound && CRForm.Profile__c==null && CRForm.Role__c==null)
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please choose role & profile'));
                return false;
            }
        }
        
        return true;
    }
    
    }

and this is the test class:

@isTest
private class CRequestForm_Wrapper_Test {

    static testmethod void constructor_Test() {
        Test.startTest();
        PageReference pageRef = Page.CRequestModify;
        Test.setCurrentPage(pageRef);
        
        SFDC_IDs__c SSAIIDs = SFDC_IDs__c.getValues('SSAI');
        Id saProfileId = SSAIIDs.System_Administrator_Profile_ID__c;
        
        User sau =
           [select UserRoleId
               from User
               where ProfileId = :saProfileId
               and isActive = true
               limit 1];
        
        System.runAs(sau) {
        
        Account newAcc=new Account(Name='Test Account');
        insert newAcc;
        
        Contact newContact=new Contact();
        newContact.LastName='Last Name';
        newContact.FirstName='First Name';
        newContact.AccountId=newAcc.Id;
        insert newContact;
        
        ApexPages.currentPage().getParameters().put('cid', newContact.Id);
        
        Contact_Request_Form__c newRec1 =new  Contact_Request_Form__c( Contact__c = newContact.Id,Last_Name__c='Last Name Test',First_Name__c='First Name Test',Email_Address__c='TEST2@MAIL.COM',Status__c='Submitted');
        insert newRec1;
        
        ApexPages.currentPage().getParameters().put('Id', newRec1.Id);
        
        ApexPages.StandardController controller = new ApexPages.StandardController(newRec1);
        CRequestForm_Wrapper WrapperC=new CRequestForm_Wrapper(controller);
        
        
        //String nextPage = WrapperC.save().getUrl();
        //System.assertNotEquals(nextPage, '');
        String nextPage = WrapperC.onApprove().getUrl();
        System.assertNotEquals(nextPage, '');
        nextPage = WrapperC.onSubmit().getUrl();
        System.assertNotEquals(nextPage, '');
nextPage = WrapperC.onReject().getUrl();
        
        
        Contact_Request_Form__c newRec2 =new  Contact_Request_Form__c( Contact__c = newContact.Id);
        
        
        ApexPages.currentPage().getParameters().put('Id', newRec2.Id);
        
        ApexPages.StandardController controller2 = new ApexPages.StandardController(newRec2);
        CRequestForm_Wrapper WrapperC2=new CRequestForm_Wrapper(controller2);
        
        
        nextPage = WrapperC2.save().getUrl();
        System.assertNotEquals(nextPage, '');
        }
        
        Test.stopTest();
    }
        

}
 


 

Best Answer chosen by Long Truong
Long TruongLong Truong
I was able to resolve this myself. Like Sonam said, you can run the test in the developer console, and it will show you what lines are not covered. I wrote code to cover those lines, mostly error catching.

All Answers

SonamSonam (Salesforce Developers) 
Did you run this class in the developer console? if not, please do so you will know exactly which lines are yet to be covered and can update the test class accordingly - going through the code will be difficult for anyone and would help if you can highlight the same ..
Long TruongLong Truong
I was able to resolve this myself. Like Sonam said, you can run the test in the developer console, and it will show you what lines are not covered. I wrote code to cover those lines, mostly error catching.
This was selected as the best answer