I was able to pass this one by modifying the classes they created as an example in the module
My class
global class LeadProcessor implements
Database.Batchable<sObject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, LeadSource FROM Lead');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
// process each batch of records
List<Lead> leads = new List<Lead>();
for (Lead lead : scope) {
lead.LeadSource = 'Dreamforce';
// increment the instance member counter
recordsProcessed = recordsProcessed + 1;
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
}
}
Test Class
@isTest
public class LeadProcessorTest {
@testSetup
static void setup() {
List<Lead> leads = new List<Lead>();
// insert 200 leads
for (Integer i=0;i<200;i++) {
leads.add(new Lead(LastName='Lead '+i,
Company='Lead', Status='Open - Not Contacted'));
}
insert leads;
}
static testmethod void test() {
Test.startTest();
LeadProcessor lp = new LeadProcessor();
Id batchId = Database.executeBatch(lp);
Test.stopTest();
// after the testing stops, assert records were updated properly
System.assertEquals(200, [select count() from lead where LeadSource = 'Dreamforce']);
}
}
I tried the code provided by you but got these error messages: Class LeadProcessor must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>) global methods do not support parameter type of List<Lead> Variable does not exist: LastName Variable does not exist: Company Variable does not exist: Status Variable does not exist: LeadSource DML requires SObject or SObject list type: List<Lead>
global class LeadProcessor implements Database.Batchable<sObject>{
global integer recordsProcessed = 1;
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator('SELECT ID, LeadSource from Lead where LeadSource = \'Dreamforce\'');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l : scope){
l.LeadSource = 'Web';
}
update scope;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
// EmailUtils.sendMessage(a, recordsProcessed);
}
}
TestClass:
@isTest
public class LeadProcessorTest {
@testSetup
static void setupLeads() {
List<Lead> leads = new List<Lead>();
// insert 200 Leads
for (Integer i=0;i<200;i++) {
leads.add(new Lead(company = 'testCompany' + i,
Leadsource='DreamForce', LastName = 'LeadingEdge' + i));
}
insert leads;
}
@isTest
static void testUpdateLeadBatch(){
List<lead> leadsForUpdate =[select id, LeadSource from Lead where LeadSource = 'Dreamforce'];
Test.startTest();
LeadProcessor lp = new LeadProcessor();
id batchJobId = Database.executeBatch(lp,200);
Test.stopTest();
List<lead> leadsAfterUpdate =[select id, LeadSource from Lead where LeadSource = 'Dreamforce'];
System.debug(leadsAfterUpdate);
System.assertEquals(0, leadsAfterUpdate.size());
}
}
Hi Ben, Thanks for the response. I tried the resolution, but somehow still getting some error messages.
Apex class LeadProcessor has these errors: Class LeadProcessor must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>)Line 1 global methods do not support parameter type of List<Lead>Line 10
Apex class LeadProcessorTest has these errors: Invalid constructor syntax, name=value pairs can only be used for SObjects: Lead Line 9 DML requires SObject or SObject list type: List<Lead>Line 12 Illegal assignment from List<Lead> to List<Lead>Line 17 Illegal assignment from List<Lead> to List<Lead>Line 24
Hi Folks, I am getting 0% code coverage for the test class LeadProcessorTest. I have tried with all the solutions listed on this forum, but I am not sure what the issue is. Seeking help from you. Kindly assist.
global class LeadProcessor implements Database.Batchable<sObject>, Database.Stateful { // instance member to retain state across transactions global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator('SELECT Id, LeadSource FROM Lead'); }
global void execute(Database.BatchableContext bc, List<Lead> scope){ // process each batch of records List<Lead> leads = new List<Lead>(); for (Lead lead : scope) { lead.LeadSource = 'Dreamforce'; // increment the instance member counter recordsProcessed = recordsProcessed + 1; } update leads; }
global void finish(Database.BatchableContext bc){ System.debug(recordsProcessed + ' records processed. Shazam!'); } }
public class LeadProcessor implements Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT LeadSource FROM Lead'
);
}
public void execute(Database.BatchableContext bc, List<Lead> records){
// process each batch of records
for(Lead lead: records){
lead.LeadSource = 'Dreamforce';
}
update records;
}
public void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}
@isTest
public class LeadProcessorTest {
@testSetup
static void setup() {
List<Lead> leads = new List<Lead>();
for(Integer i=0;i<200;i++){
leads.add(new Lead(LastName='name'+i,Company='test'));
}
insert leads;
}
@isTest static void test() {
Test.startTest();
LeadProcessor uca = new LeadProcessor();
Id batchId = Database.executeBatch(uca);
Test.stopTest();
System.assertEquals(200, [select count() from Lead where LeadSource = 'Dreamforce']);
}
}
//load test data List<Lead> leads= new List <Lead>(); for (Integer i = 0 ; i< 200; i++) { leads.add(new Lead(LastName='Patil',Company='salesforce')); } Insert leads;
//perforem test Test.startTest(); LeadProcessor lp = new LeadProcessor(); Id batchId = Database.executeBatch(lp, 200); Test.stopTest();
//check Result List<Lead> updatedLeads = [Select Id From Lead WHERE LeadSource = 'Dreamforce']; system.assertEquals(200, updatedLeads.size(), 'Error: Atleast One record is not updated correctly'); } }
global class LeadProcessor implements Database.Batchable <SObject> {
//START METHOD
global Database.QueryLocator start(Database.BatchableContext bc){
String Query='Select id,LeadSource from Lead';
return Database.getQueryLocator(Query);
}
//EXECUTE METHOD
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l: scope){
l.LeadSource='DreamForce';
}
update scope;
}
//FINISH METHOD
global void finish(Database.BatchableContext bc){
Id job= bc.getJobId();
System.debug(job);
}
}
=======================================================================================================
TEST CLASS:
@istest
private class LeadProcessorTest {
@istest
static void tetslead(){
List<Lead> l= new List<Lead>();
lead l1= new Lead();
l1.LastName='surya';
l1.Company='Company';
l1.Status='Closed-Converted';
l1.LeadSource='Dreamforce';
l.add(l1);
insert l;
Test.startTest();
LeadProcessor lp= new LeadProcessor();
Id jobid= Database.executeBatch(lp);
Test.stopTest();
}
}
All Answers
global class LeadProcessor implements Database.Batchable <SObject> {
//START METHOD
global Database.QueryLocator start(Database.BatchableContext bc){
String Query='Select id,LeadSource from Lead';
return Database.getQueryLocator(Query);
}
//EXECUTE METHOD
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l: scope){
l.LeadSource='DreamForce';
}
update scope;
}
//FINISH METHOD
global void finish(Database.BatchableContext bc){
Id job= bc.getJobId();
System.debug(job);
}
}
=======================================================================================================
TEST CLASS:
@istest
private class LeadProcessorTest {
@istest
static void tetslead(){
List<Lead> l= new List<Lead>();
lead l1= new Lead();
l1.LastName='surya';
l1.Company='Company';
l1.Status='Closed-Converted';
l1.LeadSource='Dreamforce';
l.add(l1);
insert l;
Test.startTest();
LeadProcessor lp= new LeadProcessor();
Id jobid= Database.executeBatch(lp);
Test.stopTest();
}
}
My class
Test Class
I tried the code provided by you but got these error messages:
Class LeadProcessor must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>)
global methods do not support parameter type of List<Lead>
Variable does not exist: LastName
Variable does not exist: Company
Variable does not exist: Status
Variable does not exist: LeadSource
DML requires SObject or SObject list type: List<Lead>
Could you help with resolution of these errors.
Regards
Chetan
Try this:
TestClass:
Thanks for the response. I tried the resolution, but somehow still getting some error messages.
Apex class LeadProcessor has these errors:
Class LeadProcessor must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>)Line 1
global methods do not support parameter type of List<Lead>Line 10
Apex class LeadProcessorTest has these errors:
Invalid constructor syntax, name=value pairs can only be used for SObjects: Lead Line 9
DML requires SObject or SObject list type: List<Lead>Line 12
Illegal assignment from List<Lead> to List<Lead>Line 17
Illegal assignment from List<Lead> to List<Lead>Line 24
I am getting above mentioned error while trying the code. Kindly advise.
global class LeadProcessor implements database.Batchable <sObject> {
global database.QueryLocator start(database.BatchableContext bc){
return database.getQueryLocator('Select Id, LeadSource from Lead');
}
global void execute(Database.BatchableContext bc , List<Lead> scope){
List<Lead> listLead = new List<Lead>();
For(Lead l:scope){
l.LeadSource = 'Dreamforce';
listLead.add(l);
}
update listLead;
}
global void finish(Database.BatchableContext bc){
AsyncApexJob job =[SELECT ID, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems,
CreatedBy.Email
FROM AsyncApexJob where ID =: bc.getJobId()];
System.debug(job);
}
}
============================================================
@isTest
private class LeadProcessorTest {
@isTest
private static void updateLeadProcessorTest(){
List<lead> listLead = new List<Lead>();
for(integer i; i<200; i++){
Lead leadRecord = new Lead();
leadRecord.LastName ='Hell'+i;
leadRecord.Company ='Online';
leadRecord.Status ='Open - Not Contacted';
listLead.add(leadRecord);
}
insert listLead;
}
private static testmethod void check(){
test.startTest();
LeadProcessor lPro = new LeadProcessor();
id batchId = database.executeBatch(lPro);
test.stopTest();
System.assertEquals(200, [Select count() from Lead where LeadSource='Dreamforce']);
}
}
I am getting 0% code coverage for the test class LeadProcessorTest. I have tried with all the solutions listed on this forum, but I am not sure what the issue is. Seeking help from you. Kindly assist.
global class LeadProcessor implements
Database.Batchable<sObject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, LeadSource FROM Lead');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
// process each batch of records
List<Lead> leads = new List<Lead>();
for (Lead lead : scope) {
lead.LeadSource = 'Dreamforce';
// increment the instance member counter
recordsProcessed = recordsProcessed + 1;
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
}
}
--------------------------------------------------------------------------------------------------------------
@isTest
private class LeadProcessorTest{
@testSetup
static void setup(){
List<Lead> lstOfLead = new List<Lead>();
for(Integer i = 1; i <= 200; i++){
Lead ld = new Lead(Company = 'Comp' + i ,LastName = 'LN'+i, LeadSource = 'Dreamforce', Status = 'Working - Contacted');
lstOfLead.add(ld);
}
Insert lstOfLead;
}
static testmethod void testLeadProcessor(){
Test.startTest();
LeadProcessor ldPsr = new LeadProcessor();
Id batchId = Database.executeBatch(ldPsr);
Test.stopTest();
System.assertEquals(200, [select count() from Lead where LeadSource = 'Dreamforce']);
}
}
try replacing below in your code,
LeadProcessor.apxc
LeadProcessorTest.apxc
:)
I tried the above mentioned code, but received following error message for LeadProcessor apex class:
For LeadProcessorTest apex class errors are as follows:
Kindly assist.
apxc-class
public class LeadProcessor implements Database.Batchable<sObject>, Database.Stateful {
//Instance member created to retain the state across each transaction
public Integer numberOfRecordsProcessed = 0;
//start
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, LastName, LeadSource FROM Lead');
}
//execute
public void execute(Database.BatchableContext bc, List<Lead> scope) {
List<Lead> leadListToUpdate = new List<Lead>();
for(Lead ld : scope){
if(ld.LeadSource != 'Dreamforce'){
ld.LeadSource = 'Dreamforce';
leadListToUpdate.add(ld);
numberOfRecordsProcessed += 1;
}
}if(leadListToUpdate.size() > 0){
UPDATE leadListToUpdate;
}
}
//finish
public void finish(Database.BatchableContext bc) {
System.debug('Number of records which has been processed are: '+ numberOfRecordsProcessed);
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems FROM AsyncApexJob WHERE ID =: bc.getJobId()];
System.debug('AsyncApexJob is :'+ AsyncApexJob);
}
}
apxc-class-test
@isTest
private class LeadProcessorTest {
@testSetup
static void setup() {
List<Lead> leadListToTest = new List<Lead>();
for(Integer i=0;i<200;i++){
leadListToTest.add(new Lead(lastName='Lead-'+i,LeadSource='Manforce'));
}if(leadListToTest.size() > 0){
INSERT leadListToTest;
}
}
@isTest
static void test() {
Test.startTest();
LeadProcessor batchObject = new LeadProcessor();
Id batchId = Database.executeBatch(batchObject,200);
Test.stopTest();
System.assertEquals(200, [SELECT count() FROM Lead WHERE LeadSource = 'Dreamforce']);
}
}
global class LeadProcessor implements Database.Batchable <SObject> {
//start Method
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator ('Select id,LeadSource from Lead');
}
//Execute Method
global void execute(Database.BatchableContext bc, List<Lead> scope){
List<Lead> leads= new List<Lead>();
for(Lead l: scope){
l.LeadSource='DreamForce';
leads.add(l);
}
update leads;
}
//Finish Method
global void finish(Database.BatchableContext bc){
}
Apex Test Class:
@isTest
public class LeadProcessorTest {
@testSetup
static void setupLeads() {
List<Lead> leads = new List<Lead>();
// insert 200 Leads
for (Integer i=0;i<200;i++) {
leads.add(new Lead(company = 'testCompany' + i,
Leadsource='DreamForce', LastName = 'TestLead' + i));
}
insert leads;
}
static testmethod void test(){
Test.startTest();
LeadProcessor lp = new LeadProcessor();
id batchJobId = Database.executeBatch(lp,200);
Test.stopTest();
System.assertEquals(200, [SELECT count() FROM Lead where LeadSource='DreamForce']);
}
}
public without sharing class LeadProcessor implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext dbc) {
return Database.getQueryLocator([SELECT id, Name From Lead]);
}
public void execute(Database.BatchableContext dbc, list<Lead> leads){
for(Lead l: leads) {
l.LeadSource='Dreamforce';
}
update leads;
}
public void finish(Database.BatchableContext dbc)
{
system.debug('done');
}
}
- Test Class:-
@isTestpublic class LeadProcessorTest {
@istest
private static void testBatchClass(){
//load test data
List<Lead> leads= new List <Lead>();
for (Integer i = 0 ; i< 200; i++)
{
leads.add(new Lead(LastName='Patil',Company='salesforce'));
}
Insert leads;
//perforem test
Test.startTest();
LeadProcessor lp = new LeadProcessor();
Id batchId = Database.executeBatch(lp, 200);
Test.stopTest();
//check Result
List<Lead> updatedLeads = [Select Id From Lead WHERE LeadSource = 'Dreamforce'];
system.assertEquals(200, updatedLeads.size(), 'Error: Atleast One record is not updated correctly');
}
}