0

This is the first time I am developing Apex outbound mass email class. Could anyone help how to test the class? Here is my code:

public class SendMassOutboundEmail {
public static void MassOutbMail(){

   list<Contact> conList = [select id, FirstName, Email, EmailSend__c from Contact where EmailSend__c=true and email != null];
    list<id>ids=new list<id>(); 
    for(contact con:conList){
        ids.add(con.id);

    }
    EmailTemplate emTemp = [Select id, Name, subject, body from EmailTemplate where Name=:'Noble Profit' limit 1];
   Messaging.MassEmailMessage semail=new Messaging.MassEmailMessage();
 semail.setTargetObjectIds(ids);
 semail.setSenderDisplayName('Miro Miranda');
 semail.setTemplateId(emTemp.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[]{semail});

  system.debug('Email was sent to following email adrresse'+ semail);
 }
}

Here is my unfinished test class. It gives me 84% code coverage. However, when I run this test it puts out the error message:

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target object ids (contact, lead or user): []

Test class

public class TestEmailSend3 {
public static testmethod void emailSendTest(){
  list<Contact> conList = [select id, FirstName, Email, EmailSend__c from Contact where EmailSend__c=true and email != null];

    list<id>ids=new list<id>(); 
    for(contact con:conList){
     ids.add(con.id);

    }
     Messaging.MassEmailMessage semail=new Messaging.MassEmailMessage();
     SendMassOutboundEmail.MassOutbMail();

}

}

marked as duplicate by sfdcfox unit-test Feb 25 at 1:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I've linked a few questions for you to look at. tl;dr, you need to create records for your unit test to query. – sfdcfox Feb 25 at 1:04