Salesforce 电子邮件服务单元测试
Salesforce Email Service Unit Test
可能我看这个太久了
我有一个简单的入站电子邮件服务,可以存储潜在客户并发送回复电子邮件。我的单元测试显示 0% 的覆盖率,即使我的调试语句命中并且我的系统断言通过。我做错了什么?
电子邮件服务:
global class sampleConEmailToLead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env){
// create inbound response
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
//boolean to attach note
Boolean attachNote = false;
// query leads to see if lead exist
List<Lead> leads = [SELECT Id, Name, Email
FROM Lead
WHERE Email = :email.fromAddress];
// if lead doesnt exist in system create lead
if (leads.size() == 0) {
Lead newLead = new Lead(Email = email.fromAddress,
LastName = 'From SampleCon',
Company = 'From SampleCon');
// insert lead
insert newLead;
leads.add(newLead);
attachNote = true;
} else if (leads.size() == 1){
attachNote = true;
} else {
System.debug('Duplicate Exist... not adding: ' + leads );
}
if(attachNote == true){
createNote(leads[0].id, email.subject, email.plaintextbody);
}
// set result to true or emails will bounce back
result.success = true;
//send response email
if(leads.size() > 0){
sendResponseEmail(email.fromAddress, leads[0].id);
}
// le fin
return result;
}
private void createNote(ID leadID, String subject, String body){
system.debug('### LeadID : ' + leadID);
system.debug('### Subject : ' + subject);
system.debug('### Body : ' + body);
note newNote = new note();
newNote.parentId = LeadID;
newNote.title = subject;
newNote.body = body;
try{
insert newNote;
}
catch(exception e){
system.debug('#### Error: ' + e);
}
}
private void sendResponseEmail(String email, ID leadID){
final String template = 'Test Template';
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
ID templateId = [select id from EmailTemplate where Name = :template].id;
system.debug('### templateID : ' + templateID);
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setTemplateId(templateId);
message.setTargetObjectId(LeadId);
//message.setWhatId(uc.Id);
message.setToAddresses(new String[] {email});
messages.add(message);
Messaging.sendEmail(messages);
system.debug('#### message : ' + message);
}
}
和单元测试:
@isTest(SeeAllData=true)
public class sampleConEmailToLead_Test{
static testMethod void testInboundEmail(){
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.plainTextBody = 'teset';
email.fromAddress = 'test@test.com';
email.subject = 'Test Lead';
sampleConEmailToLead edr = new sampleConEmailToLead();
edr.handleInboundEmail(email,env);
Test.startTest();
Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
System.assertEquals(result.success, true);
Test.stopTest();
}
}
将您的测试配置更新为 运行 异步。
1) Select 测试菜单。
2) Select "Always Run Asynchronously" 从下拉列表中。
3) 重新运行您的测试,整体代码覆盖率将更新,开发控制台中 Class 上的 red/blue 颜色编码也会更新。
可能我看这个太久了
我有一个简单的入站电子邮件服务,可以存储潜在客户并发送回复电子邮件。我的单元测试显示 0% 的覆盖率,即使我的调试语句命中并且我的系统断言通过。我做错了什么?
电子邮件服务:
global class sampleConEmailToLead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env){
// create inbound response
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
//boolean to attach note
Boolean attachNote = false;
// query leads to see if lead exist
List<Lead> leads = [SELECT Id, Name, Email
FROM Lead
WHERE Email = :email.fromAddress];
// if lead doesnt exist in system create lead
if (leads.size() == 0) {
Lead newLead = new Lead(Email = email.fromAddress,
LastName = 'From SampleCon',
Company = 'From SampleCon');
// insert lead
insert newLead;
leads.add(newLead);
attachNote = true;
} else if (leads.size() == 1){
attachNote = true;
} else {
System.debug('Duplicate Exist... not adding: ' + leads );
}
if(attachNote == true){
createNote(leads[0].id, email.subject, email.plaintextbody);
}
// set result to true or emails will bounce back
result.success = true;
//send response email
if(leads.size() > 0){
sendResponseEmail(email.fromAddress, leads[0].id);
}
// le fin
return result;
}
private void createNote(ID leadID, String subject, String body){
system.debug('### LeadID : ' + leadID);
system.debug('### Subject : ' + subject);
system.debug('### Body : ' + body);
note newNote = new note();
newNote.parentId = LeadID;
newNote.title = subject;
newNote.body = body;
try{
insert newNote;
}
catch(exception e){
system.debug('#### Error: ' + e);
}
}
private void sendResponseEmail(String email, ID leadID){
final String template = 'Test Template';
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
ID templateId = [select id from EmailTemplate where Name = :template].id;
system.debug('### templateID : ' + templateID);
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setTemplateId(templateId);
message.setTargetObjectId(LeadId);
//message.setWhatId(uc.Id);
message.setToAddresses(new String[] {email});
messages.add(message);
Messaging.sendEmail(messages);
system.debug('#### message : ' + message);
}
}
和单元测试:
@isTest(SeeAllData=true)
public class sampleConEmailToLead_Test{
static testMethod void testInboundEmail(){
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.plainTextBody = 'teset';
email.fromAddress = 'test@test.com';
email.subject = 'Test Lead';
sampleConEmailToLead edr = new sampleConEmailToLead();
edr.handleInboundEmail(email,env);
Test.startTest();
Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
System.assertEquals(result.success, true);
Test.stopTest();
}
}
将您的测试配置更新为 运行 异步。
1) Select 测试菜单。 2) Select "Always Run Asynchronously" 从下拉列表中。 3) 重新运行您的测试,整体代码覆盖率将更新,开发控制台中 Class 上的 red/blue 颜色编码也会更新。