I have batch Apex to send emails to Contacts which are associated with Opportunities. I want to make test class for this batch class.

My Batch class is

global class opptyDetails implements Database.Batchable<sobject>
     global Database.QueryLocator start(Database.BatchableContext bc) {
        String soql = 'SELECT Opportunity.Name, Contact.Email, Contact.Name, Contact.Level__c FROM OpportunityContactRole';
        return Database.getQueryLocator(soql);

    global void execute(Database.BatchableContext bc, List<OpportunityContactRole> recs) {

        List<Messaging.SingleEmailMessage> msgs = new List<Messaging.SingleEmailMessage>();

        for(OpportunityContactRole ocr : recs)
            Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
            msg.setSubject('Opportunity Detail');
            List<String> toAddresses = new List<String>();

            msg.setHTMLBody('Hi ' + ocr.Contact.Name + ',<br/>Below is the Opportunity Detail.<br/>Opportunity Name: ' + ocr.Opportunity.Name + '<br/><br/>Regards,<br/>Admin');




    global void finish(Database.BatchableContext bc)  {}


And here I write a test class for this batch class:

Public class TestopptyDetails
    Static TestMethod void TestopptyDetails()

Contact con = new Contact(Email='[email protected]',LastName='vivek',Level__c='5');

        insert con;
        Opportunity opp = new Opportunity(name='newopp',stagename='abc',closedate=system.today(),optname__c='abc');
        insert opp;
        OpportunityContactRole objconrole = new OpportunityContactRole(ContactID=con.id,OpportunityId = opp.id);
        List<OpportunityContactRole>  lstoppcontole = new List<OpportunityContactRole>(); 
        insert lstoppcontole;       
        opptyDetails objoppdetails = new opptyDetails();
       // database.executebatch(objoppdetails,1);
        ID batchprocessid = Database.executeBatch(objoppdetails);
        sYSTEM.debug('######EXECUTE BATCH'+batchprocessid);

In that I cover 93%, but it is not passed in the developer console. The error is:

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []

But in my test I pass the email address from the Contact. So why does this error happen?

Also here is a snapshot of code coverage from the developer console:


  • Can you please show us class where you "do job"? Because I am not sure but is it possible that you dont query for this email adress? Feb 7, 2014 at 9:51
  • Sir as a starting i add the batch apex class...
    – Vivek113
    Feb 7, 2014 at 10:12
  • Sorry I missed this - I wasn't formated well - I will fix this :) Feb 7, 2014 at 10:18
  • Sir please format my post very well this is serious bug...i can not resolve from two days...so please help me to solve this test class..
    – Vivek113
    Feb 7, 2014 at 10:31
  • 6
    I'm voting to close this question as off-topic because Abandoned Dec 28, 2016 at 4:19

2 Answers 2


There are two possibilities I think of.

1) Do you have a workflow or trigger that overwrites the email ID of a contact record? The error message says that the contact Email is 'null', so I am thinking your value is overwritten.

2) What is the version of your test class? If before 24.0, your test will see all data in your organization, and not only your test data. This can also lead to this error.

  • sir it's 29.0 version of the class.....
    – Vivek113
    Feb 7, 2014 at 12:45
  • So I think it should be option (1)
    – Dharshni
    Feb 7, 2014 at 12:46
  • Sir here i received failure in developer console and in that Error is System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []
    – Vivek113
    Feb 7, 2014 at 12:51

Wrap the line:

ID batchprocessid = Database.executeBatch(objoppdetails);


ID batchprocessid = Database.executeBatch(objoppdetails);

Does that help it?

  • Thanks for your reply...sir i tried this but it not worked....
    – Vivek113
    Feb 7, 2014 at 10:59

Not the answer you're looking for? Browse other questions tagged .