+ Start a Discussion
JoshTonksJoshTonks 

How do i write a test class for Batchable Apex

I have writen a Batchable class that works in Sandbox. Im trying to write a test class for this and am really struggling to make headway. 

This is my Class 
global class HealthUpdaterBatchable implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
        String query = 'SELECT Id FROM HealthScore__c';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<HealthScore__c> hsList) {
        for(HealthScore__c hs : hsList)
        {        
            hs.id = hs.id;
        }
        try {
        	
            update hsList;
        } catch(Exception e) {
            System.debug(e);
        }
    }   
    global void finish(Database.BatchableContext BC) {
    	
  }
}
This is my test class so far 
private class HealthUpdaterBatchableTest {

static testmethod void test() {

HealthScore__c[] hsList = new List();
for (Integer i=0;i<200;i++) {
HealthScore__c m = new Healthscore__c();
hsList.add(m);
}
insert hsList;

Test.startTest();
HealthUpdaterBatchable c = new HealthUpdaterBatchable();
Database.executeBatch(c);
Test.stopTest();
    
HealthScore__c[] hsUpdatedList = [SELECT Id FROM HealthScore__c];
System.assert(hsUpdatedList[0]);
}
}
Im getting a few errors
  • Expecting < but was ( -Line 6
  •  Invalid type: HealthScore - Line 18
  •  Method does not exist or incorrect signature: void assert(Healthscore) from the type System. - Line 19.
Any advice or assistance is very much appreciated 
 
Best Answer chosen by JoshTonks
Greg HGreg H
new Healthscore__c() is probably null. You should at least create a record with a Name attribute or something that can be populated for testing. For example,
List<HealthScore__c> hsList = new List<HealthScore__c>(); //list for test HealthScore__c records
for (Integer i=0;i<200;i++) { //while i is less than 200
	hsList.add(new Healthscore__c(Name = 'Test')); //add a HealthScore__c record to the list
}

Also, don't use the testMethod keyword. It has been depricated. Use the @isTest annotation instead.
-greg

All Answers

Alain CabonAlain Cabon

HealthScore__c[] hsList = new List <HealthScore__c> ();

List<HealthScore__c> hsUpdatedList = [SELECT Id FROM HealthScore__c];

System.assert(hsUpdatedList[0].Id != null);
Greg HGreg H
new Healthscore__c() is probably null. You should at least create a record with a Name attribute or something that can be populated for testing. For example,
List<HealthScore__c> hsList = new List<HealthScore__c>(); //list for test HealthScore__c records
for (Integer i=0;i<200;i++) { //while i is less than 200
	hsList.add(new Healthscore__c(Name = 'Test')); //add a HealthScore__c record to the list
}

Also, don't use the testMethod keyword. It has been depricated. Use the @isTest annotation instead.
-greg
This was selected as the best answer
JoshTonksJoshTonks
Thank you both you both helped me fix different parts of it now works. So thank you again for your swift answers. :)