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
Apex developer 21Apex developer 21 

Help writing a unittest for send email testclass

How do i write a unittest for the email class. I attached my code below:
public class SendemailController {
public String OpportunityId {get;set;}

Public SendemailController(){
OpportunityId = ApexPages.currentPage().getParameters().get('Id');
}

Public Pagereference sendEmailFunction(){
Opportunity getEmail = [SELECT Id, Contact__r.email FROM Opportunity WHERE id=:OpportunityId];
   if(getEmail.Contact__r.email != null) {
     String toaddress = getEmail.Contact__r.email;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toaddress};
String[] ccAddresses = new String[] {toaddress};
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
mail.setReplyTo(toaddress);
mail.setSenderDisplayName('Name');
mail.setSubject('Testing email through apex');
mail.setBccSender(false);
mail.setUseSignature(true);
mail.setPlainTextBody('Dear tester, here are the attechments. This mail is sent trough apex');

List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();        
for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :OpportunityId]){
   Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
   efa.setFileName(a.Name);
   efa.setBody(a.Body);
   fileAttachments.add(efa);
}    

mail.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
  PageReference reference = new PageReference('https://eu11.salesforce.com/'+ OpportunityId);
  reference.setRedirect(true);
  return reference;
  }
}
Best Answer chosen by Apex developer 21
pranab khatuapranab khatua
Hi,

Please follow the code:
 
@isTest
private class SendemailControllerTest{
static testMethod void test () {
	Account acc = new Account();  
	acc.Name = 'tesst';  
	insert acc;  
	Contact con = new Contact();
	con.Lastname = 'test';
	con.email = 'test@test.com';
	insert con;
	Opportunity opp = new Opportunity();
	opp.StageName = 'In Progress';
	opp.Name = 'test'; 
	opp.Contact__c = con.id;
	opp.CloseDate = System.today();
	opp.AccountId = acc.id;

	insert opp; 

	Attachment att = new Attachment();
	att.Name = 'test';
	att.Body = Blob.ValueOf('test');
	att.ParentId = opp.id;
	
	Insert att;
    
	ApexPages.currentPage().getParameters().put('Id',opp.id);	
    SendemailControllerTest Controller = new SendemailControllerTest();
   
    Controller.sendEmailFunction();
            
        
       }
}

If you get help with this please give me best answer.

Thanks,

All Answers

pranab khatuapranab khatua
Hi,

Please follow the code:
 
@isTest
private class SendemailControllerTest{
static testMethod void test () {
	Account acc = new Account();  
	acc.Name = 'tesst';  
	insert acc;  
	Contact con = new Contact();
	con.Lastname = 'test';
	con.email = 'test@test.com';
	insert con;
	Opportunity opp = new Opportunity();
	opp.StageName = 'In Progress';
	opp.Name = 'test'; 
	opp.Contact__c = con.id;
	opp.CloseDate = System.today();
	opp.AccountId = acc.id;

	insert opp; 

	Attachment att = new Attachment();
	att.Name = 'test';
	att.Body = Blob.ValueOf('test');
	att.ParentId = opp.id;
	
	Insert att;
    
	ApexPages.currentPage().getParameters().put('Id',opp.id);	
    SendemailControllerTest Controller = new SendemailControllerTest();
   
    Controller.sendEmailFunction();
            
        
       }
}

If you get help with this please give me best answer.

Thanks,
This was selected as the best answer
Apex developer 21Apex developer 21
Hi Pranab,

Nice work!!! i tested it one small piece,line 28 needs to be: SendemailController Controller = new SendemailController();