+ Start a Discussion
SunnyalexSunnyalex 

how to increase code coverage not it is 72%


public with sharing class adpContactUsController {

    public String personalizedMessage1 {get; set;}
    public String personalizedMessage2 {get; set;}
    public String personalizedMessage3 {get; set;}
    
    public adpContactUsController (){
        User u = [Select Id, ContactId from User where Id =: UserInfo.getUserId()];
        
        List<Contact> conlist = [Select Id, Advocacy_Staff_Partner__c,Advocacy_Staff_Partner__r.X18_ID__c from Contact where id =: u.ContactId]; 
                
        if(conlist.size() > 0){
            if(conlist[0].Advocacy_Staff_Partner__c == null) {
                personalizedMessage1 = defaultMessage();
            }
            else {
            ADP_StaffData__c adpStaffData = ADP_StaffData__c.getValues(conlist[0].Advocacy_Staff_Partner__c);
            if(adpStaffData != null) {
                personalizedMessage1 = adpStaffData.Personal_Message1__c;
                personalizedMessage2 = adpStaffData.Personal_Message2__c;
                personalizedMessage3 = adpStaffData.Personal_Message3__c;
            }
            
           }
        } else {
            personalizedMessage1 = defaultMessage();
        }
        
    }
    
    private String defaultMessage(){
        List<ADP_StaffData__c> adpStaffDatalist = ADP_StaffData__c.getAll().values();
            String defMsg = '';
            for(ADP_StaffData__c adpDefMsg : adpStaffDatalist){
                if(adpDefMsg.Default_Message__c) {
                   defMsg = adpDefMsg.Personal_Message1__c + ' ' + adpDefMsg.Personal_Message2__c + ' ' + adpDefMsg.Personal_Message3__c;
                   break;                 
                }
            }
            return defMsg.replaceAll('null', '');
    }
    
    public PageReference SeeAllAdvocates(){
        
        PageReference pageRef = new PageReference('/apex/AdvocatesListView');
        pageRef.setRedirect(true);
        return pageRef;
        
    }
    
    public PageReference EmailNewAdvocates(){
        
        PageReference pageRef = new PageReference('/apex/AdvocatesListView?type=new');
        pageRef.setRedirect(true);
        return pageRef;
        
    }
    
    public PageReference EmailAllAdvocates(){
        
        PageReference pageRef = new PageReference('/apex/AdvocatesListView?type=all');
        pageRef.setRedirect(true);
        return pageRef;
        
    }
    
    public PageReference createNewHouseholdContact(){
         Contact con = New Contact();
         Id Contact_HRTypeID = Schema.sObjectType.Contact.getRecordTypeInfosByName().get('Household Contact').getRecordTypeId();
         PageReference contactDetailsPage = new PageReference('/003/e');
         contactDetailsPage.getParameters().put('RecordType',Contact_HRTypeID);
         String returnURL = '/'+Page.adpHomeNew.getUrl().right(10);
         contactDetailsPage.getParameters().put('retURL', returnURL.escapeHtml3());
         contactDetailsPage.setRedirect(true);
         return contactDetailsPage;
    }
}
//////////////////////////////////////////
@isTest
public class adpContactUsControllerTest {
    
    @testSetup static void setup() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];

        fF_Location__c defaultLocation = new fF_Location__c(Name = 'Default Location', Is_Default_Unknown_Location__c = true);
        insert defaultLocation; 
        
        User u = new User(Alias = 't3stUser', Email='testUser@testkeyimpact.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='testusertest@keyimpact.com');
        
        INSERT u;
        Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Organization Contact').getRecordTypeId();
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Household').getRecordTypeId();
        Account acc = new Account();
        acc.Name = 'test account';
        acc.recordtypeid = RecordTypeIdAccount;
        insert acc;
        contact con = new contact();
        con.recordtypeid = RecordTypeIdContact;
        con.AccountId = acc.id;
        con.LastName = 'test con';
        con.Email = 'test0@test.com';
        insert con;
        
        contact con1 = new contact();
        con1.Advocacy_Staff_Partner__c = con.id; 
        con1.recordtypeid = RecordTypeIdContact;
        con1.LastName = 'test con1';
        con1.Email = 'test1@test.com';
        insert con1;
    }
    static testmethod void loginTest(){ 
        adpContactUsController adpCon =  new adpContactUsController();
        adpCon.SeeAllAdvocates();
        adpCon.EmailNewAdvocates();
        adpCon.createNewHouseholdContact();
        PageReference pageRef = adpCon.EmailAllAdvocates();
        System.assertNotEquals (null,pageRef);
    }
}
Andrew GAndrew G
Hi

Thats a bit of code and with no clear indicator on which part is not being covered it will be hard to get people to answer.  Remember TL:DR ??

But i note quickly that there are IF ....ELSE ... statements.  

You will need to insert different test data for each branch and probably have different test methods for each.


Regards
Andrew