我编写了一个Apex Batch可调度类,该类每天在特定时间运行另一个类的方法。该批处理类的作用是将所有具有“电子邮件已发送__c”字段的帐户视为真,如果为真,它将把这些客户端传递给execute函数,以便它运行来自另一个类的方法。此方法基本上会生成电子邮件并发送。发送电子邮件后,它将字段Email_Sent__c更新为false。一切工作正常,现在为了部署它,我正在编写一些测试类。问题是我为Batch Apex编写的测试类仅显示50%的覆盖率。有人可以让我知道为什么这只是50%的原因,我可以怎么做?另外,我不认为system.assert调用会在更新后检查email_sent的值,它们只是从测试设置方法中获取值
这是我写的Batch Apex类: 全局类ConfirmationCardBatchApex实现了Database.Batchable,Database.Stateful { 全局Database.QueryLocator start(Database.BatchableContext BC){ Datetime clientReleasedDate = Datetime.now()。addHours(-24);
return Database.getQueryLocator(
'SELECT Id, Client_Released__c, Client_Number__c, Release_Date__c, ' +
'Email_Sent__c FROM Account ' +
'WHERE Email_Sent__c = true AND Client_Released__c = true AND Release_Date__c<=:clientReleasedDate'
);
}
global void execute( Database.BatchableContext BC, List<Account> scope){
List<String> acctIdList = new List<String>();
for(Account account : scope){
acctIdList.add(account.Id);
}
LM_ChangeAccountRT.resendEmails(acctIdList);
} //this block of code, the execute method keeps on showing that its uncovered by my testclass!!!
global void finish(Database.BatchableContext bc){
System.debug( 'finish: called' );
}
}
我编写的测试课程如下(我确信它有很多错误。我该怎么做才能获得100%的覆盖率?)
@isTest
public class TestConfirmationCardBatchApex {
static Account client;
static Account client1;
static Account client2;
static Account parentC;
static Contact con;
@testSetup
Contact cont1 = new Contact(LastName='ContTest1', Email='test1contact@libertymutual.com', CommunicationType__c ='Holiday card', recordTypeId = Schema.SObjectType.contact.getRecordTypeInfosByName().get('Business Development Manager').getRecordTypeId(),FirstName= 'Contact');
insert cont1;
id tempuser = [Select id from User where LastName='Testing' limit 1].id;
system.debug('11111'+tempuser);
parentC = TestDataUtils.createAccountFromRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Referral Program - Participant Form').getRecordTypeId());
parentC.Dealership_Partner__c = 'BMW';
parentC.Dealership_State__c='WA';
parentC.Name = 'Parent Name';
parentC.Client_Number__c = '12302.0';
parentC.Business_Development_Manager__c=cont1.id;
insert parentC;
client1 = TestDataUTils.createAccountFromRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Referral Program - Participant Form').getRecordTypeId());
client1.Name = 'Test Client 2';
client1.Business_Development_Manager__c=cont1.id;
client1.Branch_Manager__c = tempuser;
client1.Dealership_Partner__c = 'BMW';
client1.Dealership_State__c= 'TX';
client1.ParentID = parentC.Id;
client1.Client_Number__c = '12322.0';
client1.Email_Sent__c = true;
client1.Client_Released__c = true;
accountdata.add(client1);
client2 = TestDataUTils.createAccountFromRecordType(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Referral Program - Participant Form').getRecordTypeId());
client2.Name = 'Test Client 3';
client2.Business_Development_Manager__c=cont1.id;
client2.Branch_Manager__c = tempuser;
client2.Dealership_Partner__c = 'BMW';
client2.Dealership_State__c= 'CA';
client2.ParentID = parentC.Id;
client2.Client_Number__c = '12332.0';
client2.Email_Sent__c = true;
client2.Client_Released__c = true;
accountdata.add(client2);
insert accountdata;
static testmethod void test() {
Test.startTest();
ConfirmationCardBatchApex confirm = new ConfirmationCardBatchApex();
Id batchId = Database.executeBatch(confirm);
Account emailTest = [SELECT Id, Name, Client_Released__c, Client_Number__c, Release_Date__c, Email_Sent__c FROM Account WHERE Name IN ('ContTest1', 'Test Client 1', 'Test Client 2') AND Email_Sent__c = true];
Test.stopTest();
System.assertEquals(true, emailTest.Email_Sent__c, 'Expected value should be true'); // After method is run, the email_sent__c should be false but it only shows success when I assert it to true.
}
}
如果您想知道此resendemails方法是什么(在LM_CHANGEACCOUNTRT类中),请参见以下代码片段:
@AuraEnabled
public static String resendEmails(List<String> accountIdList) {
String response = null;
try {
//Only send emails if user is either an ARMS Administor or System Administrator
if (System.label.ARMS_Administrator_Profile_Id == userinfo.getProfileId() ||
sysAdmin.Id == userinfo.getProfileId()) {
List<Account> accList = [SELECT Id,FCA_Dealership__c,Branch_Manager__c,Business_Development_Manager__c,Client_Released__c,Existing_Participant__c,
Dealership_Partner__c,Dealership_State__c,RecordTypeId,owner.email,owner.Supervisor_Email__c,Client_Number__c,Closing_Manager__r.name,
Name,Completed_Date__c,Effective_Date__c,Closing_Manager__c,Is_the_client_receiving_compensation__c,Is_a_broker_receiving_compensation__c,Broker__r.name,
,Broker__r.State__c, Email_Sent__c,
FROM Account
WHERE Id IN:accountIdList];
for(Account acc: accList){
if (acc.Client_Number__c != null && acc.Client_Released__c && acc.Email_Sent__c == true) {
sendpdfgenerationEmails(acc);
acc.Email_Sent__c = false;
response = 'Email Sent';
}else {
response= 'Access Denied';
}
}
update accList;
}
}catch(Exception e) {
System.debug(e.getMessage());
response = 'Error sending emails';
}
return response;
}//what this method does is call the sendpdfgeneration method which is the method that generates and sends a custom email. After calling the method, the email_sent__c field is updated to false
在修复我的测试方法方面的任何帮助将不胜感激。谢谢