+ Start a Discussion
Markey1Markey1 

Test Method Coverage for Apex Class

I can't seem to get the 75% test coverage required to deploy. Any help with the test code is much appreciated. Also, any links to assist with reading the debug logs (i.e. how to) would be helpful as well.

 

Code for controller with 51% coverage (psuIdRequestController):

public class psuIdRequestController {
 private String requestid;
    public Participant_ID_Request__c participantmgr;
    public String strStatus;
    public User user;
    public Contact contactBusiness;
    public Contact contactTechnical;
    public Contact contactFinance;
    public Contact contactTreasury;
    public String comment;
    public String approver;
    public Date approvalDate;
    
    
    public psuIdRequestController(ApexPages.StandardController stdController) {
        requestid = ApexPages.currentPage().getparameters().get('requestid');

        //Fetch participant for Participant ID Request record.
        participantmgr = [select Id, Name, DCF_Approval_Due_Date__c,
                        Business_DCF_Approver__c, Technical_DCF_Approver__c, Finance_DCF_Approver__c, Treasury_DCF_Approver__c,
                        Business_DCF_Approval__c, Technical_DCF_Approval__c, Finance_DCF_Approval__c, Treasury_DCF_Approval__c,
                        Business_DCF_Approval_Date__c, Technical_DCF_Approval_Date__c, Finance_DCF_Approval_Date__c, Treasury_DCF_Approval_Date__c,
                        Business_DCF_Comments__c, Technical_DCF_Comments__c, Finance_DCF_Comments__c, Treasury_DCF_Comments__c      
                        from Participant_ID_Request__c where Id =:requestid limit 1];

     try{
        contactBusiness = [select Id, Name from Contact where Id =: participantmgr.Business_DCF_Approver__c limit 1];
        contactTechnical = [select Id, Name from Contact where Id =: participantmgr.Technical_DCF_Approver__c limit 1];
        contactFinance = [select Id, Name from Contact where Id =: participantmgr.Finance_DCF_Approver__c limit 1];
        contactTreasury = [select Id, Name from Contact where Id =: participantmgr.Treasury_DCF_Approver__c limit 1];
        
        }
    catch(QueryException e){
     // System.assert(false,'You must be assigned as an approver for this participant.');    
        
        }
    
    }
  
    public Participant_ID_Request__c getparticipant(){
        return participantmgr;
    }
    
    public void Approve(){
        setStatus('Approved');      
    }
    public void Reject(){
        setStatus('Rejected');
    }
    public String getUserName(){
        return UserInfo.getName();
    }
    public String getStatus(){ 
        if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Approval__c;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Approval__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Approval__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Approval__c;
                }
            }
        return strStatus;
    }
    public void setStatus(String status){
    
        if(UserInfo.getName() == contactBusiness.Name ){
            participantmgr.Business_DCF_Approval__c = status;
            participantmgr.Business_DCF_Comments__c = comment;
            participantmgr.Business_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (UserInfo.getName() == contactTechnical.Name ){
            participantmgr.Technical_DCF_Approval__c = status;
            participantmgr.Technical_DCF_Comments__c = comment;
            participantmgr.Technical_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (UserInfo.getName() == contactFinance.Name ){
            participantmgr.Finance_DCF_Approval__c = status;
            participantmgr.Finance_DCF_Comments__c = comment;
            participantmgr.Finance_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
            if (UserInfo.getName() == contactTreasury.Name ){
            participantmgr.Treasury_DCF_Approval__c = status;
            participantmgr.Treasury_DCF_Comments__c = comment;
            participantmgr.Treasury_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
            }
        }
    }
            
    public String getParticipantName() {
        return participantmgr.Name;       
    }
    
    public String getComment(){
    
            if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Comments__c ;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Comments__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Comments__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Comments__c;
                }
                else{
                    comment = '';
                    return comment; 
                }
            }
            else{
                comment = '';
                return comment; 
            }
    }
    
    public void setComment(String value){
        comment = value;        
    }
    
    public Date getapprovalDate(){
    
            if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Approval_Date__c;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Approval_Date__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Approval_Date__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Approval_Date__c;
                }
                else{
                    approvalDate = null;
                    return approvalDate; 
                }
            }
            else{
                approvalDate = null;
                return approvalDate;
            } 
    }
    
    public String getApprover(){
            if(UserInfo.getName() == contactBusiness.Name ){
                return contactBusiness.Name;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return contactTechnical.Name;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return contactFinance.Name;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return contactTreasury.Name;
                }
                else{
                approver = 'Not Eligilble to approve';
                return approver; 
                }
            }
            else{
                approver = 'Not Eligilble to approve';
                return approver; 
            }
    }

}

 Code for test class (psuControllerTest):

@isTest
private class psuControllerTest
{
static testmethod void testPsuIdRequest() {
        Id profileid = [Select id from Profile where name = 'Customer Portal Manager Custom'].id;
        Account account = new Account(Name = 'Test Account'); 
        
        insert account; 
        Contact businessContact = new Contact(AccountID = account.id, FirstName = 'Test', LastName = 'BusinessUser', email = 'bustest@test.com' ); 
        Contact technicalContact = new Contact(AccountID = account.id, FirstName = 'Test', LastName = 'TechnicalUser', email = 'tectest@test.com' ); 
        Contact financeContact = new Contact(AccountID = account.id, FirstName = 'Test', LastName = 'FinanceUser', email = 'fintest@test.com' );
        Contact treasuryContact = new Contact(AccountID = account.id, FirstName = 'Test', LastName = 'TreasuryUser', email = 'tretest@test.com' ); 
        Contact[] contacts = new Contact[]{businessContact,technicalContact,financeContact,treasuryContact};
        insert contacts;

        User BusinessUser = new User(email='business@test.com', 
                                     contactid = businessContact.id, 
                                     profileid = profileid, 
                                     UserName='business@test.com', 
                                     alias='bususer', CommunityNickName='bususer',
                                     TimeZoneSidKey='America/New_York', 
                                     LocaleSidKey='en_US', 
                                     EmailEncodingKey='ISO-8859-1',
                                     LanguageLocaleKey='en_US', 
                                     FirstName = 'Test', 
                                     LastName = 'BusinessUser');
        User TechnicalUser = new User(email='technical@test.com', 
                                     contactid = technicalContact.id, 
                                     profileid = profileid, 
                                     UserName='technical@test.com', 
                                     alias='tecuser', CommunityNickName='tecuser',
                                     TimeZoneSidKey='America/New_York', 
                                     LocaleSidKey='en_US', 
                                     EmailEncodingKey='ISO-8859-1',
                                     LanguageLocaleKey='en_US', 
                                     FirstName = 'Test', 
                                     LastName = 'TechnicalUser');
        User FinanceUser = new User(email='finance@test.com', 
                                     contactid = financeContact.id, 
                                     profileid = profileid, 
                                     UserName='finance@test.com', 
                                     alias='finuser', CommunityNickName='finuser',
                                     TimeZoneSidKey='America/New_York', 
                                     LocaleSidKey='en_US', 
                                     EmailEncodingKey='ISO-8859-1',
                                     LanguageLocaleKey='en_US', 
                                     FirstName = 'Test', 
                                     LastName = 'FinanceUser');
        User TreasuryUser = new User(email='treasury@test.com', 
                                     contactid = treasuryContact.id, 
                                     profileid = profileid, 
                                     UserName='treasury@test.com', 
                                     alias='treuser', CommunityNickName='treuser',
                                     TimeZoneSidKey='America/New_York', 
                                     LocaleSidKey='en_US', 
                                     EmailEncodingKey='ISO-8859-1',
                                     LanguageLocaleKey='en_US', 
                                     FirstName = 'Test', 
                                     LastName = 'TreasuryUser');
        User[] users = new User[]{businessUser,technicalUser,financeUser,treasuryUser};
        insert users; 
        
        RecordType recreq = [select ID,Name from RecordType where Name = 'ID REQUEST: Draft Approved' and SObjectType = 'Participant_ID_Request__c'];

        
        Participant_ID_Request__c participantmgr = new Participant_ID_Request__c(
        recordtypeid=recreq.Id,
        Name='Test Deal1',
        OwnerId = BusinessUser.Id,
        Business_DCF_Approver__c = BusinessUser.ContactId,
        Technical_DCF_Approver__c = TechnicalUser.ContactId,
        Finance_DCF_Approver__c = FinanceUser.ContactId,
        Treasury_DCF_Approver__c = TreasuryUser.ContactId);        
        insert participantmgr;
                
        PageReference testpage = Page.psudcfreview;
        testpage.getparameters().put('requestid', participantmgr.id);
        Test.setCurrentPage(testpage);
        

        // Custom Controller
        ApexPages.StandardController con = new ApexPages.StandardController(participantmgr);
        
        // extension
        psuIdRequestController pnid = new psuIdRequestController(con);
        
        System.RunAs(BusinessUser){
        pnid.getparticipant();
        pnid.getUserName();
        pnid.getStatus();
        pnid.getParticipantName();
        pnid.getComment();
        pnid.setComment('testing');
        pnid.getapprovalDate();
        pnid.getApprover();
        pnid.approve();
        System.assertEquals('Approved', pnid.getStatus());
        }
    }
}

 

gm_sfdc_powerdegm_sfdc_powerde

How did you run your tests? If you do it from IDE, you can see the lines that are not covered by your test. You can then enhance your tests to cover those lines.