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
Ivaylo DimovIvaylo Dimov 

How to create a test class for custom controller that uses URL components

Hi,
I am trying to write a test class for a custom controller, that is using URL tags as variables.
Here is my controller:

public class LTRORequireAdditionalDocumentsCon {
    
    
    public LTRO_Document__c ltrodoc {get;set;}
    private String caseId;
    private String conEmail;
    private String contName;
    
    public List<LTRO_Document__c> ltrodoclist {get; set;}
   
    public LTRORequireAdditionalDocumentsCon(){
        
        ltrodoc = new LTRO_Document__c();
        ltrodoclist = new List<LTRO_Document__c>();
        caseId = ApexPages.currentPage().getParameters().get('cid');
        conEmail = ApexPages.currentPage().getParameters().get('conemail');
        contName = ApexPages.currentPage().getParameters().get('conname');
       
    }
    
    
    public List<SelectOption> getLtroDocTypes(){
        
        List<SelectOption> options = new List<SelectOption>();
  
        Schema.DescribeFieldResult fieldResult = LTRO_Document__c.Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
        for( Schema.PicklistEntry f : ple){
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }  
       // options.add(new SelectOption('','--None--'));
        
        return options;
    }
    
    public void addDocs(){
        if(ltrodoc.Summary__c !=''){
            LTRO_Document__c tmp = new LTRO_Document__c();
            tmp = ltrodoc.clone();
            //list
            ltrodoclist.add(tmp);
            //  docList.add(tmp);
            
            System.debug('temp :' + ltrodoclist);
            
        }
        
        else{
            System.debug('No Documents have been assigned');
        }
                
        System.debug('Documents Type: ' +ltrodoclist);
    }
   
    public void sendClientEmail(){
        
        String[] strList = new String[0];
        for(LTRO_Document__c docs: ltrodoclist){
            strList.add(String.valueOf(docs));
        }
        String result = String.join(strList, ' \n <br/>');
        System.debug('Result: ' + result);
        
        String regExp = 'Type__c=';
        String replacement = '';
        String tresult = result.replaceALL(regExp, replacement);
        System.debug('fresult: ' + tresult);
        
        String regExp1 = 'Summary__c=';
        String sresult = tresult.replaceAll(regExp1, replacement);
        System.debug('sresult: ' + sresult);
       
        String regExp2 = 'LTRO_Document__c:';
        String lresult = sresult.replaceAll(regExp2, replacement);
        System.debug('lresult: ' + lresult);
        
        String regExp3 = '[{}]';
        String cresult = lresult.replaceAll(regExp3, replacement);
        System.debug('cresult: ' + cresult);
        // Case casecon = [Select Contact.Name, Contact.Email from Case Where id =: caseId];
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        String[] toAddresses = new String[] {conEmail};
            //String[] ccAddresses = new String[] {'smith@gmail.com'};
            
            mail.setToAddresses(toAddresses);
        //        mail.setCcAddresses(ccAddresses); 
        
        mail.setReplyTo('no-reply@test.org');
        
        mail.setSenderDisplayName('Services');
        
        mail.setSubject('Assessment Application - Additional Documents are Required : ' + case.Id);
        
        mail.setBccSender(false);
        
        // Specify the text content of the email.
        mail.setPlainTextBody('We have identified that additional documents are required' + cresult
                              +' please provide the needed information'); //case.id
        
        mail.setHtmlBody('We have identified that additional documents are required<b> <br/> ' 
                         + cresult +' <br/></b> Please provide the needed information. <br/></b>'+
                        '<br><br><a href="https://link?cid={!Case.Id}">Survey</a>');
        
        // Send the email you have created.
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
    }
}
 
Raj VakatiRaj Vakati
try this demo
 
@isTest
public class LTRORequireAdditionalDocumentsConTest {

    public static testMethod void testMyController() {
	
	LTRO_Document__c ob = new LTRO_Document__c();
	ob.Name='Test';
	ob.Type__c='';
	// add other fields 
	insert ob ;
	
	
	
        PageReference pageRef = Page.success;
        Test.setCurrentPage(pageRef);
      System.currentPageReference().getParameters().put('cid', ob.id);
System.currentPageReference().getParameters().put('conemail', 'test@gmai.com');
System.currentPageReference().getParameters().put('conname', 'test');

        LTRORequireAdditionalDocumentsCon  controller = new LTRORequireAdditionalDocumentsCon ();
        controller.getLtroDocTypes();
		        controller.addDocs();
						        controller.sendClientEmail();



    }
}