+ Start a Discussion
AichaSFAichaSF 

Apex test Class _ Page Reference

Hello, I have a visualforce page:
<apex:page lightningStylesheets="true"   standardController="Contact" extensions="LCN01_ContactFoundCTI" action="{!LCN01_ContactFoundCTI1}"> 
</apex:page>
Below its apex class:
public with sharing class LCN01_ContactFoundCTI{
    String conId;
    String stdcon;
    String phone;
    public Contact con {get;set;}
    
 public LCN01_ContactFoundCTI(ApexPages.StandardController stdcon) {
        phone = ApexPages.CurrentPage().getParameters().get('phone');
     system.debug('phone '+phone);
        if (conId != null && conId != '') // if id is in url means record is for edit 
            con = (Contact) stdcon.getRecord();
        else // else you r gonna create new record
            con = new Contact();
    }
         
    public PageReference LCN01_ContactFoundCTI1(){
        string contactphone;
        string contid;
        string decode;
      
        String phone = System.currentPageReference().getParameters().get('phone');
        string UrlGeodis = label.LABS_SF_UrlGeodis;
        string UrlContactNonExistant = label.LABS_SF_LCUrlContactNonExistant;
		 String strAssumptionFloodOnlyUrl;
        system.debug('phone  '+phone);
         try {           
            contactphone= '+' +phone;           
            contactphone= contactphone.replaceAll( '\\s+', '');
            system.debug('phone2 '+contactphone);
            //Recupetation Contact Id
            list<Contact> cont = [SELECT id, Name from Contact where Phone = :contactphone or MobilePhone= :contactphone  ];  
              list<Delegue__c> Deleguee = [SELECT id, Name from Delegue__c where Telephone__c = :contactphone  ]; 
            string today = date.today().format();
            system.debug('cont  '+cont.size());
            system.debug('Deleguee  '+Deleguee.size());
			
            if (cont != null &&  cont.size() == 1 && Deleguee.size() == 0)
            {
              
               task tache = new task(); 
                tache.OwnerId= UserInfo.getUserId() ;
                tache.Subject = 'Appel Entrant ' +today;
                tache.WhoId	= cont[0].id; 
                contid = cont[0].id; 
                system.debug('contact existant');
                insert tache; 
                system.debug('tache id  '+tache.id);
                
		        strAssumptionFloodOnlyUrl =UrlGeodis + '/r/Contact/'+contid+'/view';
            } 
            else if (Deleguee != null &&  Deleguee.size() == 1 && cont.size() == 0)
            {
               
				task tache = new task(); 
                tache.OwnerId= UserInfo.getUserId() ;
                tache.Subject = 'Appel Entrant ' +today;
                tache.WhoId	= Deleguee[0].id; 
                contid = Deleguee[0].id; 
                system.debug('Deleguee existant');   
              	
	           strAssumptionFloodOnlyUrl =UrlGeodis + '/r/Delegue__c/'+contid+'/view';
				
            } 
             else if (Deleguee.size() == 0 && cont.size() == 0) {
                   system.debug('ContactNotFound');
                 strAssumptionFloodOnlyUrl= UrlContactNonExistant;
                         }
		   // si il y a plusieurs contacts ou plusieurs délégués
            else  {
                system.debug('plusieurscontacts');
                //decode   
                string decode1 ='{"componentDef":"forceSearch:searchPage","attributes":{"term":"' +phone+'","scopeMap":';
                string decode2 ='{"resultsCmp":"forceSearch:resultsTopResults","label":"Principaux résultats","type":"TOP_RESULTS","cacheable":"Y","id":"TOP_RESULTS","labelPlural":"Principaux résultats"}}}';
                
                Decode = decode1 +    decode2 ;
                system.debug('Decode '+Decode); 
                
                // create a blob from our parameter value before we send it as part of the url
                Blob beforeblob = Blob.valueOf(Decode);                
                // base64 encode the blob that contains our url param value
                string paramvalue = EncodingUtil.base64Encode(beforeblob);
                system.debug('paramvalue  '+paramvalue);
                // print out the encoded value to the debug log so we can see it before/after base64 encode
                System.debug(Decode + ' is now encoded as: ' + paramvalue);                    
               
               string Searchlabel = label.LABS_SF_Global_Search +paramvalue ;
			   strAssumptionFloodOnlyUrl= label.LABS_SF_Global_Search +paramvalue ;
               contid =  Searchlabel;              
            }				 
		          
          PageReference newFloodOnlyUrl = new PageReference(strAssumptionFloodOnlyUrl);
          newFloodOnlyUrl.setRedirect(true);
          return newFloodOnlyUrl;          
            
        }
        catch(Exception ex){ 
        system.debug('erreur  '+ex);         
        return null;          
            
        }
    }
   
 
}
I created a test class but it does not cover the class:
@isTest
public class LCN01_ContactFoundCTITest {
    public static testMethod void testContact() {   
        test.startTest();
          PageReference pageRef = Page.VF01_ContactFoundCTI;
          Account acc = new Account(Name='Abce');
          insert acc;
          
          Contact  testContact = new Contact();
          testContact.lastname='testcontact';
          testContact.AccountId=acc.id;
          testContact.phone='+33612345690';
          
           insert testContact;
           Test.setCurrentPage(pageRef);
           pageRef.getParameters().put('id',testContact.id);
           ApexPages.StandardController sc = new ApexPages.standardController(testContact);
           LCN01_ContactFoundCTI  controller = new LCN01_ContactFoundCTI(sc);
         
   
                
       test.stopTest();
        

}
    
}


Can someone help me?
Martha VMartha V
Aisha, after you instantiate your controller extention, you need to call the methods in the class and check that the variables or the returns are what you expect. 

so call the method:
 
PageReference result =  controller.LCN01_ContactFoundCTI1();

and then test the result:
System.assertEquals(null, result);  //or whatever return value you are expecting.

Test it with different values in your testContact object to make sure it will pass through all the if's and else's your method has.