+ Start a Discussion
Abhishek Kumar 149Abhishek Kumar 149 

how to write test class fro the below code?

public PageReference sendEmail(){
            if(String.isBlank(toEmail)) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Please enter To Address'));
                return null;
            if(String.isBlank(subject)) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Please enter Subject'));
                        return null;                
Mahesh Dhara 9Mahesh Dhara 9
The point is that as emails are not sent in tests asserting what has happened is awkward; one way around that is to create a class that the trigger uses to send:
Anjum Attar 26Anjum Attar 26
ApexPages.Message[] pageMessages = ApexPages.getMessages();
System.assertNotEquals(0, pageMessages.size()); // Check that the error message you are expecting is in pageMessages
Boolean messageFound = false;
for(ApexPages.Message message : pageMessages)
if(message.getSummary() == 'Your summary' && message.getDetail() == 'Your detail' && message.getSeverity() == ApexPages.Severity.YOUR_SEVERITY) {
messageFound = true;