function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
swain 10swain 10 

Batch Apex Test Coverage

global class EcommerceToUnitOfUseInShowingsBatchJob implements Database.Batchable<sObject> {
    
    String query;
    private List<Id> showingIds;
    private List<Showing__c> showingObj;
    
    global EcommerceToUnitOfUseInShowingsBatchJob() {
        
    }
    
    global EcommerceToUnitOfUseInShowingsBatchJob(List<Showing__c> parameter) {
     showingObj=parameter;   
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'SELECT Id, Account__c, Ecommerce__c, Use_of_Unit__c From Showing__c where Ecommerce__C !=null  and Unit__r.RecordType.Name=\'Active\'';
        if (null != showingIds && !showingIds.isEmpty()) {
            query += ' ANd Id IN :showingIds';
        }
        return Database.getQueryLocator(query);
    }

       global void execute(Database.BatchableContext BC, List<sObject> scope) {
           Map<Id,Showing__c> showingsToUpdate = new Map<Id,Showing__c>();
        List<Showing__c> showingsIds = (List<Showing__c>) scope;

        for( Showing__c shwng : showingsIds)
        {
            List<Account> accounts = [SELECT Id from Account WHERE Id=:shwng.Account__c];
            if (!accounts.isEmpty() || null == shwng.Account__c) {
                if('Yes'.equals(shwng.Ecommerce__c)) {
                    shwng.Use_of_Unit__c='E-Commerce';
                    showingsToUpdate.put(shwng.Id,shwng);
                } else if('No'.equals(shwng.Ecommerce__c) || 'Unknown'.equals(shwng.Ecommerce__c)) {
                    shwng.Use_of_Unit__c='';
                    showingsToUpdate.put(shwng.Id,shwng);
                }
               }
        }

        if (!showingsToUpdate.isEmpty()) {
            update(showingsToUpdate.values());    
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
}
Raj VakatiRaj Vakati
try this code
 
@isTest
private class EcommerceToUnitOfUseInShowingsBatchJobTest {
@testSetup 
static void setup() {
	
	//Account__c
	
	Account acc = new Account(name='Account '+i, 
			billingcity='New York', billingcountry='USA');
			// add other required fields 
		insert acc ;	
			
			
			Ecommerce__C  ec = new Ecommerce__C () ;
			// add other required fields 
			ce.Name ='Yes';
			insert ec ; 
			
			Ecommerce__C  ec1 = new Ecommerce__C () ;
			// add other required fields 
			ec1.Name ='No';
			insert ec1 ; 
			
	 Id recTypeId = Schema.SObjectType.Unit__c.getRecordTypeInfosByName().get('Active').getRecordTypeId();

			Unit__c uc = new Unit__c() ;
			// add other required fields 
			uc.Name ='Test';
			uc.RecordTypeId = recTypeId ;
			insert uc ;
			
			
   List<Showing__c > sws = new List<Showing__c >();

	for (Integer i=0;i<10;i++) {
		sws.add(new Showing__c(name='Showing '+i, 
			Use_of_Unit__c =10, Ecommerce__C =ec.id ,Unit__c =uc.Id));
	}
	
	for (Integer i=0;i<10;i++) {
		sws.add(new Showing__c(name='Showing '+i, 
			Use_of_Unit__c =10, Ecommerce__C =ec1.id ,Unit__c =uc.Id));
	}
	insert sws;
	
}
static testmethod void test() {        
	Test.startTest();
	EcommerceToUnitOfUseInShowingsBatchJob  ecBatch = new EcommerceToUnitOfUseInShowingsBatchJob ();
	Id batchId = Database.executeBatch(ecBatch);
	Test.stopTest();
  
}

}