+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

Test class not covering execute() method in batch class

global class LoanOfficerBatch implements Database.Batchable<sObject> {
    public String query = 'SELECT Loan_Officer_1a__c,Loan_Officer_1a__r.Email, Name, Phone, Starting_Credit_Score__c, ' 
                          +  'Status, Enrolled_On__c, Est_Re_Pull_Date__c, Realtor_Name__c ' 
                          +   ' FROM Lead';
    public EmailTemplate templateId = [Select Id,HtmlValue,Subject from EmailTemplate where name = 'LoanOfficerRecord' LIMIT 1];

    global Database.QueryLocator start(Database.BatchableContext bc) {

        query += ' WHERE CreatedDate >= LAST_MONTH AND CreatedDate <= THIS_MONTH AND Loan_Officer_1a__c != null';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Lead> allLeads) {
        Map<Id,List<Lead>> leadMap = new Map<Id,List<Lead>>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMEssage>();
        if(allLeads != null && allLeads.size() > 0){
            for(Lead l: allLeads){
                if(!leadMap.containsKey(l.Loan_Officer_1a__c)){
                    leadMap.put(l.Loan_Officer_1a__c, new List<lead>());
                }
                leadMap.get(l.Loan_Officer_1a__c).add(l);
            }
        }
        if(leadMap.keySet().size() > 0){
            Map<Id,Contact> officers = new Map<Id,Contact>([SELECT Id,Email,Name FROM Contact WHERE Id IN: leadMap.keySet()]);
            for(Id i: leadMap.keySet()){
                Contact con = officers.get(i);
                System.debug(con);
                if(String.isnOtBlank(con.Email)){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(new String[]{con.EMail});
                    mail.setSubject(templateId.Subject);
                    String html = templateId.HtmlValue;
                    html = html.replace('||OfficerName||',con.Name);
                    String leadsTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+
                        '<tr style="font-weight:bold;"><td>Name</td><td>Phone</td><td>Starting Credit Score</td><td>Status</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>';
                    for(Lead l: leadMap.get(i)){
                        leadsTable += '<tr><td>'+l.Name+'</td>'+
                            '<td>'+l.Phone+'</td><td>'+l.Starting_Credit_Score__c+'</td><td>'+l.Status+'</td><td>'+l.Enrolled_On__c+'</td>'+
                            '<td>'+l.Est_Re_Pull_Date__c+'</td><td>'+l.Realtor_Name__c+'</td></tr>';
                    }
                    leadsTable += '</table>';
                    html = html.replace('||Leads||',leadsTable);
                    html = html.replace('null',' ');
                    mail.setHTMLBody(html);
                    mails.add(mail);
                }
            }
        }
        if(mails.size() > 0){
            //Messaging.sendEmail(mails);
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
   
}

I'm tring to write test class following is the test class;
@isTest
private class LoanOfficerBatchTest {

	static testMethod void exTest1(){
		
		
		 Date startDate = Date.today().addMonths(-1);
        startDate = Date.newInstance(startDate.year(),startDate.month(),1);
        Date endDate = startDate;
        Integer days = Date.daysInMonth(endDate.year(), endDate.month());
		
		Contact c   = new Contact();
       c.FirstName = 'Stephen';
       c.LastName  = 'Curry';
       c.Email     = 'stephcurry@gsw.com';
       insert c;
	   
		Lead ls =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' ,Loan_Officer_1a__c =c.Id);
		insert ls ;
		Test.setCreatedDate(ls.Id, startDate);

		
		Lead ls1 =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' );
		insert ls1 ;
		Test.setCreatedDate(ls1.Id, startDate);

	test.startTest();

	
		test.stopTest();
	}
}

I'm not covering a code coverage of 75%. It is not increasing more than 17% please suggest. 
Best Answer chosen by Muhammad Jawwad 16
Raj VakatiRaj Vakati
Try this
@isTest
private class LoanOfficerBatchTest {

	static testMethod void exTest1(){
		 Date startDate = Date.today().addDays(-25);
    	Contact c   = new Contact();
       c.FirstName = 'Stephen';
       c.LastName  = 'Curry';
       c.Email     = 'stephcurry@gsw.com';
       insert c;
	   
		Lead ls =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' ,Loan_Officer_1a__c =c.Id);
		insert ls ;
		Test.setCreatedDate(ls.Id, startDate);

		
		Lead ls1 =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' );
		insert ls1 ;
		Test.setCreatedDate(ls1.Id, startDate);

		
		
		Test.startTest();
		Database.executeBatch(new LoanOfficerBatch ()); 
		Test.stopTest();
	}
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
@isTest
private class LoanOfficerBatchTest {

	static testMethod void exTest1(){
		
		
		 Date startDate = Date.today().addMonths(-1);
        startDate = Date.newInstance(startDate.year(),startDate.month(),1);
        Date endDate = startDate;
        Integer days = Date.daysInMonth(endDate.year(), endDate.month());
		
		Contact c   = new Contact();
       c.FirstName = 'Stephen';
       c.LastName  = 'Curry';
       c.Email     = 'stephcurry@gsw.com';
       insert c;
	   
		Lead ls =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' ,Loan_Officer_1a__c =c.Id);
		insert ls ;
		Test.setCreatedDate(ls.Id, startDate);

		
		Lead ls1 =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' );
		insert ls1 ;
		Test.setCreatedDate(ls1.Id, startDate);

	test.startTest();
            Database.executeBatch(new LoanOfficerBatch ());

	
		test.stopTest();
	}
}

 
Muhammad Jawwad 16Muhammad Jawwad 16
 Its not working i'm tring to much time Database.executeBatch(new LoanOfficerBatch ()); i thing code is not working
Muhammad Jawwad 16Muhammad Jawwad 16
Please see the whole batch class and suggest me solution plzzz
Raj VakatiRaj Vakati
Can you do me small help .. 
  1. Use my code what i shared as is 
  2. Check whether test class is running successfully or not ..If its failing give me why its uts failing 
  3. Give me a screenshot with which lines are not covering .. we can able to fix it 
Muhammad Jawwad 16Muhammad Jawwad 16
User-added image
Muhammad Jawwad 16Muhammad Jawwad 16
Execute method not working in test class
Raj VakatiRaj Vakati
Try this
@isTest
private class LoanOfficerBatchTest {

	static testMethod void exTest1(){
		 Date startDate = Date.today().addDays(-25);
    	Contact c   = new Contact();
       c.FirstName = 'Stephen';
       c.LastName  = 'Curry';
       c.Email     = 'stephcurry@gsw.com';
       insert c;
	   
		Lead ls =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' ,Loan_Officer_1a__c =c.Id);
		insert ls ;
		Test.setCreatedDate(ls.Id, startDate);

		
		Lead ls1 =new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open' );
		insert ls1 ;
		Test.setCreatedDate(ls1.Id, startDate);

		
		
		Test.startTest();
		Database.executeBatch(new LoanOfficerBatch ()); 
		Test.stopTest();
	}
}

 
This was selected as the best answer
Muhammad Jawwad 16Muhammad Jawwad 16
you are great sir thank you so much working with 100% coverage.