Activity Forums Salesforce® Discussions How to write test class for outbound email services

  • Radhakrishna

    Member
    July 14, 2017 at 10:35 am

    what do you actually want to test? the execution of the code or that the message gets delivered?

    how about you delegate it to a separate method sendNotication() which you can test independently. furthermore you can test it via console as well

    batch class

    global class MyBatch implements Database.Batchable<sObject> {

    // BATCH CONTRACT ...

    global void finish(Database.BatchableContext bc){

    if (MY_CONDITION)
    {
    sendNotification();
    }
    }

    public void sendNotification(){

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    // CONFIGURE MAIL

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

    }

    test class

    @isTest
    private class MyBatchTest {
    static testMethod void sendNotificationTest(){

    Test.startTest();
    Integer emailbefore = Limits.getEmailInvocations();
    MyBatch batch = new MyBatch();
    MyBatch.sendNotication();

    system.assertNotEquals(emailbefore,Limits.getEmailInvocations(),'should have decreased');
    Test.stopTest();
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos