You need to sign in to do that
Don't have an account?
Afzaal Hassan
Apex Test Coverage showing 50% only
Hello
I have written an Apex Batch schedulable class that runs a method from another class everyday at a certain time. What this batch class does is see all the accounts that have a field "Email Sent__c" as true and if it is true, it will pass those clients to the execute function so that it runs a method from a different class. this method basically generates an email and sends it. Once the email is sent, it updates the field Email_Sent__c to false. Everything works great and now in order to deploy it, I am writing some test classes. The problem is that the test class I have written for the Batch Apex only shows 50% coverage. Can someone please let me know why this is only 50% and what I can do to fix it? Also, I dont think the system.assert calls are checking the email_sent values after the update, they are only taking the values from the test setup method
Here is the Batch Apex class I wrote:
global class ConfirmationCardBatchApex implements Database.Batchable<sObject>, Database.Stateful {
global 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);
}
global void finish(Database.BatchableContext bc){
System.debug( 'finish: called' );
}
}
The test class I wrote is the following (I am sure it has alot of mistakes. what can i do to get 100% coverage?)
@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.
}
}
In case you are wondering what this resendemails method is(in the LM_CHANGEACCOUNTRT Class), here is a snippet of it:
@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 methid, the email_sent__c field is updated to false
Any help in fixing my test method would be great;y appreciated. Thank you
I have written an Apex Batch schedulable class that runs a method from another class everyday at a certain time. What this batch class does is see all the accounts that have a field "Email Sent__c" as true and if it is true, it will pass those clients to the execute function so that it runs a method from a different class. this method basically generates an email and sends it. Once the email is sent, it updates the field Email_Sent__c to false. Everything works great and now in order to deploy it, I am writing some test classes. The problem is that the test class I have written for the Batch Apex only shows 50% coverage. Can someone please let me know why this is only 50% and what I can do to fix it? Also, I dont think the system.assert calls are checking the email_sent values after the update, they are only taking the values from the test setup method
Here is the Batch Apex class I wrote:
global class ConfirmationCardBatchApex implements Database.Batchable<sObject>, Database.Stateful {
global 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);
}
global void finish(Database.BatchableContext bc){
System.debug( 'finish: called' );
}
}
The test class I wrote is the following (I am sure it has alot of mistakes. what can i do to get 100% coverage?)
@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.
}
}
In case you are wondering what this resendemails method is(in the LM_CHANGEACCOUNTRT Class), here is a snippet of it:
@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 methid, the email_sent__c field is updated to false
Any help in fixing my test method would be great;y appreciated. Thank you
Raj Vakati
Try thus
Afzaal Hassan
@Raj Vakati This didnt work. And I am getting syntax errors. When I fixed the syntax errors, I am still getting 50% code coverage