You need to sign in to do that
Don't have an account?
SV M
Test class for Batch Apex is not running
Hi, I have written a batch class to send email to contact owner with Contact details created by lead conversion. I have tried Test class for the same but it was not running. Can someone help me resolve this.
//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
public List<Contact> conList {get;set;}
public String body{get;set;}
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
//system.debug('aaaaaaa'+query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, Lead[] scope) {
List<Contact> conList = new List<Contact>();
for(Lead lead : scope) {
conList = ([SELECT FirstName, LastName, Email, Phone
FROM Contact
WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
}
system.debug('Contacts List'+conList);
String.join(conList,',');
messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
body = 'Contact Details : ' +conList+ '';
email.setPlainTextBody(body);
email.setSubject('Contact Details from Converted Lead');
email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
//system.debug('Contacts'+conList);
}
global void Finish(Database.BatchableContext BC) {
}
}
//Test Class
@isTest
public class EmailWithAttachment_TC {
public static testMethod void EmailWithAttachment_TCMethod() {
Lead led = new Lead (
LastName = 'Test Lead',
Company = 'Test Company',
Status = 'Open - Not Contacted'
);
insert led;
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(led.Id);
lc.setDoNotCreateOpportunity(false);
lc.setConvertedStatus('Converted');
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}
}
//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
public List<Contact> conList {get;set;}
public String body{get;set;}
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, FirstName, LastName, LeadSource, ConvertedDate isConverted FROM Lead';
//system.debug('aaaaaaa'+query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, Lead[] scope) {
List<Contact> conList = new List<Contact>();
for(Lead lead : scope) {
conList = ([SELECT FirstName, LastName, Email, Phone
FROM Contact
WHERE Id IN (SELECT ConvertedContactId FROM Lead)]);
}
system.debug('Contacts List'+conList);
String.join(conList,',');
messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
body = 'Contact Details : ' +conList+ '';
email.setPlainTextBody(body);
email.setSubject('Contact Details from Converted Lead');
email.setToAddresses(new string[]{'maddulasaivineeth@gmail.com'});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
//system.debug('Contacts'+conList);
}
global void Finish(Database.BatchableContext BC) {
}
}
//Test Class
@isTest
public class EmailWithAttachment_TC {
public static testMethod void EmailWithAttachment_TCMethod() {
Lead led = new Lead (
LastName = 'Test Lead',
Company = 'Test Company',
Status = 'Open - Not Contacted'
);
insert led;
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(led.Id);
lc.setDoNotCreateOpportunity(false);
lc.setConvertedStatus('Converted');
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}
}
Please add the following lines to your test class.
Test.startTest();
EmailWithAttachment obj = new EmailWithAttachment();
DataBase.executeBatch(obj);
Test.stopTest();
Let me know if your still not able to get the code coverage