salesforce - 电子邮件类的 Apex 测试用例

标签 salesforce apex-code visualforce

我有以下类(class)来发送电子邮件

global class SendConfirmation {

 public SendConfirmation(ApexPages.StandardController controller)
 {
 }

 Webservice static void SendEmail(string contactId,string oppId)
 {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId(contactId);
    mail.setWhatId(oppId);
    mail.setTemplateId('00Xd0000000PFaY');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 }
}

上面的课很好

但无法获得以下测试用例来完成 eclipse 中的代码覆盖

@isTest
private class SendConfirmationTestCase {
private static testMethod void myUnitTest() {
    Contact con =  new Contact();
    con.FirstName = 'Anil';
    con.LastName = 'Dutt';
    con.Email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e3ecebeec2f1f5ebe4f6f1e7f6f7f2ace1edef" rel="noreferrer noopener nofollow">[email protected]</a>';
    insert con;


    Opportunity oppNew =  new Opportunity();
    oppNew.Name = 'Test Opp';
    oppNew.StageName = 'Ticketing';
    oppNew.CloseDate = System.now().date();
    insert oppNew;


   //ApexPages.StandardController sc = new ApexPages.StandardController(con);
   //SendConfirmation sc1=new SendConfirmation (sc);
   //sc1.SendEmail();
}
}

如果我注释掉测试用例中的最后 3 行

出现以下错误

SendConfirmationTestCase:无效类型:SendConfirmation

预先感谢您的帮助..

最佳答案

试试这个,我的测试结果是 100%。

global class SendConfirmation 
{
    public SendConfirmation(ApexPages.StandardController controller)
    {
    }

    Webservice static void SendEmail(string contactId,string oppId)
    {
        Messaging.SingleEmailMessage mail 
            = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(contactId);
        mail.setWhatId(oppId);
        // assuming this Template ID exists in your org
        mail.setTemplateId('00Xd0000000PFaY'); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

    private static testMethod void myUnitTest() 
    {
        Contact con =  new Contact();
        con.FirstName = 'Anil';
        con.LastName = 'Dutt';
        con.Email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a6a9aeab87b4b0aea1b3b4a2b3b2b7e9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>';
        insert con;

        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'Ticketing';
        oppNew.CloseDate = System.now().date();
        insert oppNew;

        ApexPages.StandardController sc 
            = new ApexPages.StandardController(con);
        SendConfirmation sc1=new SendConfirmation (sc); // test constructor

        // Not: sc1.SendEmail(); 
        // Because method is a webservice in a global class
        SendConfirmation.SendEmail(con.Id,oppNew.Id);
    }
}

关于salesforce - 电子邮件类的 Apex 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10039223/

相关文章:

salesforce - 无法从 Salesforce Einstein Bot 调用操作

angularjs - 如何在 Salesforce 中使用 Angular 指令

Salesforce:在测试类中全面避免州长限制

salesforce - 在Salesforce Apex中解析JSON对象

java - 合并正则表达式中的两个条件

url - Salesforce 创建按钮和链接选项 - 用于启动电子邮件模板的自定义 URL

api - 带有外部 ID 的 upsert 如何工作?

salesforce - 我安排了每天进行顶点类(class),但什么也没发生。有人可以帮我吗?

Salesforce 页面在数据库更新时自动更新

javascript检查这种格式的字符串