我已经在Apex写了一个工作课。这是一个电子邮件服务扩展器,用于处理传入的电子邮件。 它在我的沙箱环境中工作得很完美。我在apex force.com代码中的测试课有什么问题?
我创建了一个测试类,所以我也可以将其部署到我的生产,但验证码的时候,我得到我的代码只有72%的测试。
这是我的主类
global class inboundEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Lead lead;
String [] mFromUserParams;
String [] sourceText;
String mCaseObject;
try{
sourceText = email.toAddresses[0].split('@');
String [] mParams = sourceText[0].split('\\.');
**// FROM THIS LINE TO THE END - NOT COVERED**
mFromUserParams = email.fromAddress.split('@');
mCaseObject = mParams[0];
if (mCaseObject == 'lead'){
lead = new Lead();
lead.LastName = mFromUserParams[0];
lead.Company = email.fromAddress;
lead.OwnerId = mParams[1];
lead.LeadSource = mParams[2];
lead.Email = email.fromAddress;
lead.RequirementsDescription__c = email.subject + email.plainTextBody;
insert lead;
result.success = true;
} else if (mCaseObject == 'case'){
result.success = true;
} else {
result.success = false;
}
}catch(Exception e){
result.success = false;
result.message = 'Oops, I failed.';
}
return result;
}
}
这是我的测试类
@isTest
private class inboundEmailTest {
public static testMethod void inboundEmail(){
// Create a new email, envelope object and Header
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
envelope.toAddress = '[email protected]';
envelope.fromAddress = '[email protected]';
email.subject = 'Please contact me';
email.fromName = 'Test From Name';
email.plainTextBody = 'Hello, this a test email body. for testing Bye';
// setup controller object
inboundEmail catcher = new inboundEmail();
Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, envelope);
}
}
根据该错误消息,从所述第三线的try/catch块ALL线没有被盖住。 (在代码中标出)。
是啊,我还以为过,但信封没有toAddresses []对象。只作为单个字符串对象。 – Saariko 2012-07-05 16:41:52
你不需要信封对象,只需设置'email.toAddresses =新名单 {“[email protected]”};' –
h9nry
2012-07-05 18:06:50
我发现,我用inboundEmail作为类名,但InboundEmail是一个内部对象。也许它干扰或不干扰。也从email.fromAddress中删除LIST解决了这个问题。谢谢 – Saariko 2012-07-08 07:51:32