Salesforce 如何为Messaging.SingleEmailMessage类编写测试类?

Salesforce 如何为Messaging.SingleEmailMessage类编写测试类?,salesforce,apex-code,test-coverage,Salesforce,Apex Code,Test Coverage,我编写了以下Apex类,该类处理发送到电子邮件服务地址的传入电子邮件,并从传入邮件创建新任务,然后将此新任务与salesforce中的匹配记录关联。对记录名和传入电子邮件主题进行匹配。该类还发送一封电子邮件,通知“分配给”用户他们已经收到了对正在处理的请求的答复 这在沙箱中非常有效,但我没有编写测试类的经验。有谁能建议我如何编写下面的测试类吗 global class RequestEmailHandler implements Messaging.InboundEmailHandler { g

我编写了以下Apex类,该类处理发送到电子邮件服务地址的传入电子邮件,并从传入邮件创建新任务,然后将此新任务与salesforce中的匹配记录关联。对记录名和传入电子邮件主题进行匹配。该类还发送一封电子邮件,通知“分配给”用户他们已经收到了对正在处理的请求的答复

这在沙箱中非常有效,但我没有编写测试类的经验。有谁能建议我如何编写下面的测试类吗

global class RequestEmailHandler implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    String myPlainText = email.plainTextBody;
    String subject = email.fromName + ' - ' + email.subject;
    system.debug(email);

    subject = subject.left(255);
    Request__c request;


    if (subject != null && subject.trim().length() > 0 && subject.indexOf('(R-') > 0) {
        Integer idx = subject.indexOf('(R-');
        String requestName = subject.substring(idx+1, subject.indexOf(')', idx));
        request = [SELECT Id, Assigned_To__c FROM Request__c WHERE Name = :requestName];
    }

    if (request == null) {
        result.message = 'We were unable to locate the associated request.This may be due to the unique "R" number being removed from the subject line.\n Please include the original email subject when replying to any emails.';
        result.success = false;
        return result;
    }            

    // Add the email plain text into the local variable       
    Task task = new Task(
       WhatId = request.Id,
       Description =  myPlainText,
       Priority = 'Normal',
       Status = 'Completed',           
       Type = 'Email',
       Subject = subject,
       ActivityDate = System.today(),
       RecordTypeId = '01250000000HkEw');
    insert task;


    //Find the template
    EmailTemplate theTemplate = [select id, name from EmailTemplate where DeveloperName = 'New_Email_Reply2'];
    //Create a new email right after the task
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    //Add email To addresses to list
    List<String> toAddresses = new List<String>();
    toAddresses.add(email.fromAddress);
    //Set the list of to addresses
    mail.setToAddresses(toAddresses);
    //Set the template id
    mail.setTemplateId(theTemplate.id);
    //The Id of the user
    mail.setTargetObjectId(request.Assigned_To__c);
    //Set the id of the request
    mail.setWhatId(request.Id);
    //If you need the email also saved as an activity, otherwise set to false
    mail.setSaveAsActivity(false);   

    //Send Email
    try {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
    catch (EmailException e) {
        system.debug('sendEmail error: ' + e.getMessage());
    }


    // Save attachments, if any
    if (email.textAttachments != null)
    {
        for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
        {
            Attachment attachment = new Attachment();

            attachment.Name = tAttachment.fileName;
            attachment.Body = Blob.valueOf(tAttachment.body);
            attachment.ParentId = request.Id;
            insert attachment;
        }

    }

    //Save any Binary Attachment
    if (email.binaryAttachments != null)
    {
        for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
            Attachment attachment = new Attachment();

            attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
            attachment.ParentId = request.Id;
            insert attachment;    
            return result;
        }
    }
    return result;
}

第一步是确定测试类没有覆盖哪些代码行

如果您使用的是Eclipse,那么可以从Apex Test Runner视图中看到这一点

或者,您也可以从开发人员控制台看到这一点

您需要考虑的另一件事是在一个单独的实用程序类中隔离您的DML操作。

public class TestUtils
{
   // create request objects method here

   // create task objects method here
}
另外,检查调试日志,确保代码没有抛出任何异常(例如,空指针异常、DML异常等)

您还必须添加断言以检查代码是否按预期运行


希望这有帮助。

您需要做的主要事情是通过单元测试测试尽可能多的用例。 因此,为特定案例设置数据并运行电子邮件处理。发送电子邮件后,使用
System.assertEquals()
检查结果。对每个用例进行单独的测试。 然后,如果你没有击中至少75%,检查什么是不涵盖的。您可能不需要该代码(如果您涵盖了所有用例),或者不为使用这些代码行的用例编写测试

public class TestUtils
{
   // create request objects method here

   // create task objects method here
}