I'm trying to write a test class for my controller extension. In this extension I used addFields to add custom fields which i already filter with !Test.isRunningTest(), so it wont cause an error during the Testrun. The problem is I actually need the fields in the logic.
The if clause I use:
if(methodNum == '1' && header.TP_Kopf_1_Zentrierung__c + header.TP_Kopf_1__c.length() + spaceShift < 68)
The test comes up to the if, but only throws a 'Attempt to de-reference a null object' afterwards.
My Extension Constructor:
public ChangeSpacingController(ApexPages.StandardController stdController){
if(!Test.isRunningTest()) stdController.addFields(new List<String>{'TP_Kopf_1__c','TP_Kopf_1_Zentrierung__c','TP_Kopf_2__c','TP_Kopf_2_Zentrierung__c','TP_Kopf_3__c','TP_Kopf_3_Zentrierung__c','TP_Kopf_4__c','TP_Kopf_4_Zentrierung__c','TP_Kopf_5__c','TP_Kopf_6__c','TP_Kopf_6_Zentrierung__c','Zeilen_zentrieren__c'});
this.header = (TP_Header__c)stdController.getRecord();
}
Current state of Testclass:
PageReference tpageRef = Page.TPKopf_ChangeSpacing;
Test.setCurrentPage(tpageRef);
Account testAcc = new Account(Name = 'TestAccount', ao_Invoice_Type__c = 'Test', ax_lob__c = 'Test');
insert testAcc;
TP_Header__c testHeader = new TP_Header__c(Name = 'TestHeader',
TP_Kopf_1__c = 'Test1',
TP_Kopf_2__c = 'Test2',
TP_Kopf_3__c = 'Test3',
TP_Kopf_4__c = 'Test4',
TP_Kopf_5__c = 'Test5',
TP_Kopf_6__c = 'Test6',
Zeilen_zentrieren__c = false,
account__c = testAcc.ID);
insert testHeader;
ApexPages.StandardController sc = new ApexPages.StandardController(testHeader);
ChangeSpacingController spController = new ChangeSpacingController(sc);
spController.methodNum = '1';
spController.addFive();
I also tried to insert the addFields into the Testclass itself which only gave me the error:
'You cannot call addFIelds when the data is being passed into the controller by the caller.'