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
Kevin LanguedocKevin Languedoc 

How to Write a Test case

I have this code and I am not sure to test. Do I need to replace the SELECT by an actual Case id? Do I need to create a case via Apex and populate to required fiedls to send page as pdf?
 
public class Form1747MailerController {
	 
    private Case c;
  
    
    
    public Form1747MailerController(){
        c = [SELECT id, CaseNumber, Subject, Description FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('id')  ];
    }
    
  	public void SendPDF(){
        String[] sendToAddr;
        sendToAddr[0] = c.CaseAssignedOwner__c;
  		Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
  		PageReference pr = Page.Form1747web;
        pr.getParameters().put('id', c.Id);
        pr.setRedirect(true);
        Blob docBlob = pr.getContent();
        
        Messaging.EmailFileAttachment emailatt = new Messaging.EmailFileAttachment();
        emailatt.setFileName(c.CaseNumber);
        emailatt.setBody(docBlob);
        
        msg.setSubject(c.Subject);
        msg.setPlainTextBody(c.Description);
        msg.setToAddresses(sendToAddr);
        
        msg.setFileAttachments(new Messaging.EmailFileAttachment[]{emailatt});
      
  		List<Messaging.SendEmailResult> results = 
    					Messaging.sendEmail(new Messaging.Email[] { msg });
		if (!results.get(0).isSuccess()) {
    			System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
    		String errorMessage = results.get(0).getErrors()[0].getMessage();
		} 	
        
  }
      
}

 
Rajiv Bhatt 16Rajiv Bhatt 16
1. You will have to create a case record such that the query returns at least one record 
2. You will have to set the page parameters first in the test class and then instantiate the controller and execute other methods.
PageReference pageRef = Page.YourVFPageName;
pageRef.getParameters().put('id', provideCaseIdHere);
Test.setCurrentPage(pageRef);
// instantiate controller
// invoke controller methods