You need to sign in to do that
Don't have an account?
JoshTonks
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
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.
Also, don't use the testMethod keyword. It has been depricated. Use the @isTest annotation instead.
-greg
All Answers
HealthScore__c[] hsList = new List <HealthScore__c> ();
List<HealthScore__c> hsUpdatedList = [SELECT Id FROM HealthScore__c];
System.assert(hsUpdatedList[0].Id != null);
Also, don't use the testMethod keyword. It has been depricated. Use the @isTest annotation instead.
-greg