3

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>();

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

            msgs.add(msg);
        }

        if(!msgs.isEmpty())
        {
            messaging.sendEmail(msgs);
        }

    }

    global void finish(Database.BatchableContext bc)  {}

}

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

@isTest
Public class TestopptyDetails
{
    Static TestMethod void TestopptyDetails()
{  

Contact con = new Contact(Email='vivek.istarmca@gmail.com',LastName='vivek',Level__c='5');

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

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:

Coverage

  • 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? – Artur Kępczyński Feb 7 '14 at 9:51
  • Sir as a starting i add the batch apex class... – Vivek113 Feb 7 '14 at 10:12
  • Sorry I missed this - I wasn't formated well - I will fix this :) – Artur Kępczyński Feb 7 '14 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 '14 at 10:31
  • 6
    I'm voting to close this question as off-topic because Abandoned – Tushar Sharma Dec 28 '16 at 4:19
1

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.

| improve this answer | |
  • sir it's 29.0 version of the class..... – Vivek113 Feb 7 '14 at 12:45
  • So I think it should be option (1) – Dharshni Feb 7 '14 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 '14 at 12:51
0

Wrap the line:

ID batchprocessid = Database.executeBatch(objoppdetails);

with

Test.startTest();
ID batchprocessid = Database.executeBatch(objoppdetails);
Test.stopTest();

Does that help it?

| improve this answer | |
  • Thanks for your reply...sir i tried this but it not worked.... – Vivek113 Feb 7 '14 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.