我已經在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