电子邮件类的Apex测试用例

时间:2012-04-06 04:45:28

标签: salesforce apex-code visualforce

我有以下课程发送电子邮件

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

ablove class很好

但无法在eclipse中完成以下测试用例以完成代码覆盖

@isTest
private class SendConfirmationTestCase {
private static testMethod void myUnitTest() {
    Contact con =  new Contact();
    con.FirstName = 'Anil';
    con.LastName = 'Dutt';
    con.Email = 'anil@swiftsetup.com';
    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

先谢谢你的帮助..

1 个答案:

答案 0 :(得分:4)

试试这个,它为我测试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 = 'anil@swiftsetup.com';
        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);
    }
}