9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テストクラスにApex電子メール送信の検証方法

Last updated at Posted at 2019-05-30

テストメソッドが実行完了すると、トランザクションは自動的にロールバックされます。
テストクラスからApexメール送信機能を呼び出して、実際に送信しないことです。

テストクラスに Limits.getEmailInvocations を使用してApex電子メール送信されるかどうかを確認できます。

Apex電子メール送信機能

public with sharing class EmailUtility {
    public static void sendPlainTextEmail(List<String> toAddresses, String subject, String body) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.settoAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
テストクラス(Apex電子メール送信確認)

@isTest
private class EmailUtilityTest {
    @isTest
    static void testEmailUtility() {
        Test.StartTest();
        
        EmailUtility.sendPlainTextEmail(new List<String>{'test@test.com'}, 'Unit Test X', 'Unit Test');
        
        Test.stopTest();
        
        // Apex電子メール送信検証
        System.assertEquals(1, Limits.getEmailInvocations());
    }
}
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?