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
Andrew Hoban 6Andrew Hoban 6 

Trouble creating a test class

I have a class that needs a test class. 

I was wondering could someone help me create a test class for the class below?

I have been stuggling with this class fora  while help would be miuch appreciated.

Thanks 
 
global class TalentIntCustomerBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
	global final String query;

	global TalentIntCustomerBatch(String q){
		query=q;
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC){
		return Database.getQueryLocator(query);
	}

	global void execute(Database.BatchableContext BC, List<sObject> scope){		
		for(sObject s : scope){
			Contact c = (Contact)s;
			TalentIntegrationUtils.updateCustomer(c.Id, c.LastName);
		}
	}
	
	global void finish(Database.BatchableContext BC){}
}

 
Best Answer chosen by Andrew Hoban 6
MithunPMithunP
Hi Andrew,

Try this updated one
@isTest  (SeeAllData = true)                              
public class TestBatchClass {
    public static testmethod void test() {
     
       Test.startTest();
          string query = 'Select id,lastname,email from contact';  // you can update your query here
          TalentIntCustomerBatch batchObj = new TalentIntCustomerBatch(query);
          Database.executeBatch(batchObj);
       Test.stopTest();
      
    }
}
Best Regards,
Mithun.
 

All Answers

MithunPMithunP
Hi Andrew,

You can write something like this. (Or) try this
@isTest  (SeeAllData = true)                              
public class TestBatchClass {
    public static testmethod void test() {
     
       Test.startTest();
          TalentIntCustomerBatch batchObj = new TalentIntCustomerBatch();
          batchObj.query = 'Select id,lastname,email from contact'  // you can update your query here
          Database.executeBatch(batchObj);
       Test.stopTest();
      
    }
}

Best Regards,
Mithun.
Andrew Hoban 6Andrew Hoban 6
Thank you for the reply.

I am getting the error message:

Error: Compile Error: Constructor not defined: [TalentIntCustomerBatch].<Constructor>() at line 6 column 45
MithunPMithunP
Hi Andrew,

Try this updated one
@isTest  (SeeAllData = true)                              
public class TestBatchClass {
    public static testmethod void test() {
     
       Test.startTest();
          string query = 'Select id,lastname,email from contact';  // you can update your query here
          TalentIntCustomerBatch batchObj = new TalentIntCustomerBatch(query);
          Database.executeBatch(batchObj);
       Test.stopTest();
      
    }
}
Best Regards,
Mithun.
 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thanks a lot!
MithunPMithunP
I'm glad it helped.

Best Regards,
Mithun.