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

Pros & Cons of Lightning Component vs. 3rd party frameworks in Salesforce UI development

Video in

Nikita drops by to share his invaluable experience of using popular 3rd party frameworks in Salesforce. http://media.blubrry.com/salesforceway/audio.xmcdn.com/group42/M05/25/57/wKgJ81rXk6XjaZIAAU_I_KtprFM998.m4a