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
Chad CoffeyChad Coffey 

Help building a Test Class

I have an Apex Class that is deleting a large number of record based on a SOQL query that is entered.  I am having a very hard time getting a TestClass Built.  Any help would be greatly appreciated!

Batch Class:
global class TestBatch implements Database.Batchable<sObject>{
   global final String Query;

   global TestBatch(String q){
             Query=q;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
          delete scope;
   }

   global void finish(Database.BatchableContext BC){

   }

}


When I run the following script in the console, the records associated with the object are deleted. 

String query = 'SELECT Id FROM jstcl__PlacementTeamMember__c';

Database.executeBatch(new TestBatch(query), 10000);
v varaprasadv varaprasad
Hi Chad,

Please check once below sample code : 
@isTest
private class DeletePlacementTeamMember_sTest{
@testSetup 
static void setup() {
	List<jstcl__PlacementTeamMember__c> pmrs = new List<jstcl__PlacementTeamMember__c>();
  
	// insert 10 accounts
	for (Integer i=0;i<10;i++) {
		pmrs.add(new jstcl__PlacementTeamMember__c(name='PMR'+i));
	}
	insert pmrs;
   
}
static testmethod void test() {   
    String query = 'SELECT Id FROM jstcl__PlacementTeamMember__c';
	Test.startTest();
	TestBatch uca = new TestBatch(query);
	Id batchId = Database.executeBatch(uca);
	Test.stopTest();
	
}

}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Raj VakatiRaj Vakati
try this 
..  add all required fields 
@isTest
public class TestBatchTest {
    @isTest static void check(){
        list<jstcl__PlacementTeamMember__c> c=new list<jstcl__PlacementTeamMember__c>();
        for(integer i=0;i<200;i++){
            jstcl__PlacementTeamMember__c g=new jstcl__PlacementTeamMember__c();
            g.Name='ABSYZ';
// add other required fields 
            c.add(g);
        }
        insert c;
           
  Test.startTest();
      TestBatch  x = new TestBatch ('SELECT Id FROM jstcl__PlacementTeamMember__c');
    database.executeBatch(x);
    Test.stopTest();
    }
}

 
Chad CoffeyChad Coffey
Thank you Raj!! Worked like a charm.