I am getting a strange error when running my test method:
@isTest
public class Batch_Ent_Update_Eqn_Status_TESTMETHOD{
public static testmethod void testBatch_Ent_Update_Eqn_Status(){
//List<cif_entity__c> lstEntities = [Select Name, ID_CIF__c, CIF_id__c, Equation_Status__c from cif_entity__c where equation_status__c = :'Updating Equation'];
//system.debug('number before = ' + lstEntities.size());
//System.assertEquals(1, lstEntities_Before.size(),'BEFORE = ' + lstEntities_Before.size());
test.startTest();
BATCH_Ent_Update_Eqn_Status myClass = new BATCH_Ent_Update_Eqn_Status();
myClass.lnumber = 'No Limit';
database.executebatch(myClass,1);
test.stopTest();
}
}
The test fails every time with a message System.NullPointerException: Attempt to de-reference a null object
and the stacktrace given is:
Class.BATCH_Ent_Update_Eqn_Status.start: line 13, column 1
But I know that the code has run because I have received an email with the results in a csv file.
Here is the batch class I am testing which sends the email:
global class BATCH_Ent_Update_Eqn_Status implements Database.Batchable<SObject>, Database.Stateful {
private integer RecordNo = 0;
public string scope = '1';
public String lnumber = 'No Limit';
public string updatedEntities = '';
public string csvData = '';
global Database.QueryLocator start(Database.BatchableContext context) {
system.debug('start');
Bulk_Address_Switch__c bas = Bulk_Address_Switch__c.getInstance('isBulkUpdate');
bas.Field_Value__c = 'Yes';
update bas;
RecordNo = 0;
system.debug('set qry');//Updating Equation
string query =
'Select Name, ID_CIF__c, CIF_id__c, Equation_Status__c from cif_entity__c where equation_status__c =\'Updating Equation\'';
if(lnumber != 'No Limit')query += ' limit '+lnumber;
system.debug('return QUERY STRING:');
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext context, List<cif_entity__c> scope) {
system.debug('in execute');
updatedEntities='';
Datetime myDatetime = Datetime.now();
String myDatetimeStr = myDatetime.format('dd/MM/yyyy');
system.debug('date = ' + myDatetimeStr);
system.debug('start loop through scope');
for(cif_entity__c c:scope){
system.debug('in loop through query, #' + RecordNo);
updatedEntities += string.valueOf(c.Name)+',' + string.valueOf(c.ID_CIF__c)+',' + myDatetimeStr + ',' +c.Equation_Status__c +',';
c.Equation_Status__c = 'Equation Updated'; //
updatedEntities += c.Equation_Status__c + '\n';
system.debug('Entity record for: ' + c.Name);
RecordNo++;
update scope;
csvData +=updatedEntities;
system.debug('Update records record no: ' + RecordNo);
}
system.debug( RecordNo + ' records updated!');
system.debug(csvData);
}
/**
* @description gets invoked when the batch job finishes. Place any clean up code in this method.
* @param context contains the job ID
*/
global void finish(Database.BatchableContext context) {
//Set Is bulkUpdate to No
system.debug('set Bulk_Address_Switch to No');
Bulk_Address_Switch__c bas = Bulk_Address_Switch__c.getInstance('isBulkUpdate');
bas.Field_Value__c = 'No';
update bas;
string csvHeader='Name, Cif ID, Last Modified Date, old Equation Status, new Equation Status\n';
csvData = csvHeader + csvData;
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
//Create csv attachment
blob csvBlob = Blob.valueOf(csvData); //updatedEntities
string csvname= 'UpdatedEntityrecords.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
//Create Email
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'[email protected]'};
String[] ccAddresses = new list<string> {'[email protected]'};
email.setSenderDisplayName('Entity records Equation status Batch job');
email.setSubject('Entity records Equation status updated');
email.setToAddresses( toAddresses );
email.setCcAddresses( ccAddresses );
email.setPlainTextBody('Please see the attached file');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
it essentially finds all the records in our custom object that have a certain status set in a field, and updates them...
So what am I doing wrong in my test method, I cannot even begin to look for code overage until it even works...
System.assetEquals
to check the results...do I need to create a basic record as part of my test, then call the method on that?test.startTest()
or before?