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
sai kumar 433sai kumar 433 

please help test class for this thanks in advance

global class UpdateContactAddresses 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, BillingStreet, BillingCity, BillingState, ' +
            'BillingPostalCode, (SELECT ID, MailingStreet, MailingCity, ' +
            'MailingState, MailingPostalCode FROM Contacts) FROM Account ' + 
            'Where BillingCountry = \'USA\''
        );
    }

    global void execute(Database.BatchableContext bc, List<Account> scope){
        // process each batch of records
        List<Contact> contacts = new List<Contact>();
        for (Account account : scope) {
            for (Contact contact : account.contacts) {
                contact.MailingStreet = account.BillingStreet;
                contact.MailingCity = account.BillingCity;
                contact.MailingState = account.BillingState;
                contact.MailingPostalCode = account.BillingPostalCode;
                // add contact to list to be updated
                contacts.add(contact);
                // increment the instance member counter
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update contacts;
    }    

    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
            }    

}
Best Answer chosen by sai kumar 433
Nayana KNayana K
@isTest
private class UpdateContactAddressesTest {

    static testmethod void test() {
        Account objAcc = new Account(Name = 'Test Acc', BillingState = 'AP', BillingStreet='Tst', BilllingCity='test city',
									BillingCountry='USA', BillingPostalCode='12345');
		insert objAcc;
		
		Contact objCon = new Contact(LastName = 'Test Con', AccountId = objAcc.Id);
		insert objCon;
		

       Test.startTest();
       Database.executeBatch(new UpdateContactAddresses());
       Test.stopTest();
		
		// verify the result
       System.assertEquals('AP',[SELECT MailingState FROM Contact WHERE Id=: objCon.Id].MailingState);
    }
}

 

All Answers

Nayana KNayana K
@isTest
private class UpdateContactAddressesTest {

    static testmethod void test() {
        Account objAcc = new Account(Name = 'Test Acc', BillingState = 'AP', BillingStreet='Tst', BilllingCity='test city',
									BillingCountry='USA', BillingPostalCode='12345');
		insert objAcc;
		
		Contact objCon = new Contact(LastName = 'Test Con', AccountId = objAcc.Id);
		insert objCon;
		

       Test.startTest();
       Database.executeBatch(new UpdateContactAddresses());
       Test.stopTest();
		
		// verify the result
       System.assertEquals('AP',[SELECT MailingState FROM Contact WHERE Id=: objCon.Id].MailingState);
    }
}

 
This was selected as the best answer
sai kumar 433sai kumar 433
perfect..........