2

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

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 课很好

但无法获得以下测试用例来完成 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

在此先感谢您的帮助..

4

1 回答 1

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);
    }
}
于 2012-04-06T13:35:43.873 回答