+ Start a Discussion
Sumit  61Sumit 61 

Execute Method is not covering in batch class

When I running batch class its not covering excute method and giving 38%
 
global class Batch implements Database.Batchable<sObject>, Schedulable  {
    public static final String STATUS_SCHEDULED = 'Scheduled';  
    public static final String STATUS_SUBMITTED = 'Submitted';

    public static final Set<String> SET_SUBMITTED_TIMECRAD_STATUS =  new Set<String>{STATUS_SUBMITTED, STATUS_APPROVED, STATUS_REJECTED};
        
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query ='SELECT Id, Name,Contact__c, Contact__r.Email ,Contact__r.Name, ';
               query += ' (SELECT Id FROM Contact__r WHERE Start_Date__c = LAST_N_DAYS:12 AND Status__c IN: SET_SUBMITTED_TIMECRAD_STATUS )';
               query += ' FROM Account WHERE Status__c =: STATUS_SCHEDULED AND Contact__c != NULL';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope) {
		
		Map<Id,Account> contactIdWithAssigment = new Map<Id,Account>();		
		
		for(Account  assigment : scope) {
		   if(assigment.Contact__r.size() == 0) {
		   	 contactIdWithAssigment.put(assigment.Contact__c, assigment); 
		   }									        	
		}
        
        String startDate = Date.Today().addDays(-6).format();
        String endDate = Date.Today().format();
        MTX_TimecardReminder_Uitility.sendTimecardReminder('Timecard_Reminder_Template', contactIdWithAssigment, startDate, endDate);
    }
    
    global void finish(Database.BatchableContext BC) {      
  	}
  	
  	global void execute(SchedulableContext sc) {
        MTX_TimecardReminder_Batch timecardReminder = new MTX_TimecardReminder_Batch(); 
        database.executebatch(timecardReminder);
    }
}
 
@isTest
public class test_Batch_Test {
    
    @testSetup 
    static void setup() {
        List<Project__c> dem = new List<Project__c>();
        List<Account> acc = new List<Account>();
        List<Contact> con = new List<Contact>();
        
        // insert 10 records
        for (Integer i=0;i<10;i++) {
            dem.add(new Project__c(name='Project '+i, 
                Project_Stage__c ='New'));
        }
        insert dem;
        
        for (Project__c demByList : [SELECT Id from Project__c]){
			acc.add(new Account(Name = 'TestName', Status__c = 'Scheduled',
                 Project__c=demByList.id));
        }
        insert acc;
        
        for (Account accByList : [SELECT Id,Project__c from Account]){
			con.add(new Contact(AccountId = accByList.Id,
                 Status__c = 'Submitted', Start_Date__c = System.today() - 12,
                 Project__c=accByList.Project__c));
        }
        insert con;
        

        
    }
    
    static testmethod void test() {        
        
        Test.startTest();
        test_Batch trb = new test_Batch();
        Id batchId = Database.executeBatch(trb, 200);
        Test.stopTest();

    }


}

 
pradeep kumar yadavpradeep kumar yadav
User-added image
What are doing?

you have to initialize main batch class for testing.

And also data in testSetup should be according to conditions used in a query in batch start method.

And one thing if you insert some data in testSetup you can easily get id like:---

Account acc = new Account(AccountName='ABC');
insert acc;

you can use acc.Id to create contact.
Madan Khadka 2Madan Khadka 2
Hi Sumit 61,

You seem to have call the wrong class from the test class. Please see below :

static testmethod void test() { 
     Test.startTest();
      BatchClassName objBatch = new BatchClassName(); (in your case the classname is Batch)
      Id batchId = Database.executeBatch(objBatch,200);
      Test.stopTest();
}