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

How to write test method for this send email method?

Visualforce code:


<apex:form ><br />
<apex:outputLabel value="Subject" for="Subject"/>:<br />
<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
<br /><br />
<apex:outputLabel value="Body" for="Body"/>:<br />
<apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
<br /><br /><br />
<apex:commandButton id="sendEmail" value="Send Email" action="{!send}" />


Apex Code:


public PageReference send() {
// Define the email

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

String[] addresses = new string[]{''};

// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( addresses );
email.setPlainTextBody( body );

// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

return null;


Best Answer chosen by Admin (Salesforce Developers) 


private class testClass {

    //test send method

    private void testMethod testSend(){

    Class controller = new Class(); // where class is the name of class in which Send() method is defined.


    PageReference page = controller.Send();

    System.assert(page == null);

    //validate other things such as user, addresses, logic.





hope this will help you.