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
Anonymous DeveloperAnonymous Developer 

Help Test Class Im Stuck

Here's my code

Apex Class:
global class ASOverrideContacRegion implements Database.Batchable<sObject>{
    List<Contact> conlist = new List<Contact>();
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT MailingPostalcode,MailingCity,Region_Override__c,Region__c FROM Contact WHERE MailingPostalcode != null AND Region_Override__c = FALSE';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Contact> batch) {
        System.debug('batch=='+batch);
            
        Set<String> postcodes = new Set<String>();
        for (Contact a : batch) {
            postcodes.add(a.MailingPostalcode);
        } 
            
        Map<String,Id> mapregcode = new Map<String,Id>();
        for(Region__c reg: [SELECT Id,Postcode__c,Suburb_Name__c FROM Region__c WHERE Postcode__c IN :postcodes]) {
            mapregcode.put(reg.Postcode__c,reg.Id);
        }

        for(Contact con : batch) {
            if(mapregcode.containskey(con.MailingPostalcode)) {        
                Contact conTemp = new Contact();
                conTemp.Id = con.Id;
                conTemp.Region__c = mapregcode.get(con.MailingPostalcode);
                conlist.add(conTemp);
            }
        }

        System.debug('postcodes >>>> ' +postcodes);
        System.debug( 'mapregcode >>>>>' +mapregcode );
        update conlist;
        
        system.debug('conlist=='+conlist);
    }

    global void finish(Database.BatchableContext BC) {
    
    }
                                               
}


Test Class:

@isTest
public class ASOverrideContacRegionTest {
    
     
    static testMethod void setup(){
    List<Contact> tempConList = new List<Contact>();
        Contact con = new Contact();
      con.FirstName = 'Erin';
      con.LastName = 'Daly';
            con.MailingCity = 'Brisbane';
            con.MailingPostalCode = '4000'; 
            con.Region_Override__c = TRUE; 
      tempConList.add(con);
        insert tempConList;
    update tempConList;

    Contact con1 = new Contact();
      con1.FirstName = 'Erin';
      con1.LastName = 'Daly';
      con1.MailingPostalCode = '4008';
    insert con1;


        Region__c reg = new Region__c();
      // con.FirstName = 'Erin';
      // con.LastName = 'Daly'; 
            reg.Suburb_Name__c = 'Brisbane'; 
            reg.Postcode__c = '4000';
        insert reg;

        
      Test.startTest();
      ASOverrideContacRegion OCR = new ASOverrideContacRegion();
      Id jobId = Database.executeBatch(OCR);
      Test.stopTest();

    }

}

User-added image
Best Answer chosen by Anonymous Developer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
Just a small chanfe is required as highlited below.
 
@isTest
public class ASOverrideContacRegionTest {
    
     
    static testMethod void setup(){
    List<Contact> tempConList = new List<Contact>();
        Contact con = new Contact();
      con.FirstName = 'Erin';
      con.LastName = 'Daly';
            con.MailingCity = 'Brisbane';
            con.MailingPostalCode = '4000'; 
            con.Region_Override__c = False; 
      tempConList.add(con);
        insert tempConList;
    update tempConList;

    Contact con1 = new Contact();
      con1.FirstName = 'Erin';
      con1.LastName = 'Daly';
      con1.MailingPostalCode = '4008';
    insert con1;


        Region__c reg = new Region__c();
      // con.FirstName = 'Erin';
      // con.LastName = 'Daly'; 
            reg.Suburb_Name__c = 'Brisbane'; 
            reg.Postcode__c = '4000';
        insert reg;

        
      Test.startTest();
      ASOverrideContacRegion OCR = new ASOverrideContacRegion();
      Id jobId = Database.executeBatch(OCR);
      Test.stopTest();

    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
Just a small chanfe is required as highlited below.
 
@isTest
public class ASOverrideContacRegionTest {
    
     
    static testMethod void setup(){
    List<Contact> tempConList = new List<Contact>();
        Contact con = new Contact();
      con.FirstName = 'Erin';
      con.LastName = 'Daly';
            con.MailingCity = 'Brisbane';
            con.MailingPostalCode = '4000'; 
            con.Region_Override__c = False; 
      tempConList.add(con);
        insert tempConList;
    update tempConList;

    Contact con1 = new Contact();
      con1.FirstName = 'Erin';
      con1.LastName = 'Daly';
      con1.MailingPostalCode = '4008';
    insert con1;


        Region__c reg = new Region__c();
      // con.FirstName = 'Erin';
      // con.LastName = 'Daly'; 
            reg.Suburb_Name__c = 'Brisbane'; 
            reg.Postcode__c = '4000';
        insert reg;

        
      Test.startTest();
      ASOverrideContacRegion OCR = new ASOverrideContacRegion();
      Id jobId = Database.executeBatch(OCR);
      Test.stopTest();

    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Anonymous DeveloperAnonymous Developer
Thanks for the help @Sai Praveen
yumona joeyumona joe

When running Apex Test in org credentials, you may notice that some of the Apex Test ... the message below will be displayed in the classes: https://innoutsecretmenu.online/