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
richfer_rightrichfer_right 

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:form>

 

Apex Code:

 

public PageReference send() {
// Define the email

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

String[] addresses = new string[]{'richard@supremetechnologies.in'};

Messaging.reserveSingleEmailCapacity(addresses.size());
// 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) 
Parth_SevakParth_Sevak

@IsTest

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.

    @test.startTest();

    PageReference page = controller.Send();

    System.assert(page == null);

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

    @test.stopTest();

    }

}

 

hope this will help you.