You need to sign in to do that
Don't have an account?
Test Class Problems with System.NullPointerException: Attempt to de-reference a null
OK, gonna keep this short ... I am new to Apex and especially writing test classes is giving me a headache ... HELP PLEASE!!!
Here is the class ... (took out some code and replaced with ... for easier reading)
public class DoPaymentHandler{
public DoPaymentHandler(){
}
public DoPaymentHandler(ApexPages.StandardController controller) {
this.reg = (Registration__c)controller.getRecord();
}
...
//Handling payment processing submitting by VF page button
public void process(){
isSuccess = false;
amount = String.valueOf(reg.Open_Amount__c);
ponumber = reg.Event__r.PO_Number__c;
description = reg.Event__r.Name;
firstname = reg.Participant__r.First_Name__c;
lastname = reg.Participant__r.Last_Name__c;
email = reg.Participant__r.Email__c;
state = reg.Participant__r.Billing_State__c;
zip = reg.Participant__r.Billing_ZIP__c;
cardnumber = reg.Participant__r.Credit_Card__c;
expiration = reg.Participant__r.Expiration_Date__c;
if(!validate()){
isSuccess = false;
return;
}
AuthorizeDotNet ath = new AuthorizeDotNet(isTest);
ath.transaction(cardnumber, expiration, amount, '','','');
ath.setParameter('x_device_type','1');
ath.setParameter('x_first_name',firstname);
ath.setParameter('x_last_name',lastname);
// ath.setParameter('x_address',address);
ath.setParameter('x_state',state);
ath.setParameter('x_response_format','1');
ath.setParameter('x_zip',zip);
ath.setParameter('x_po_num',ponumber);
ath.setParameter('x_description',description);
try{
ath.process(3);
}catch(Exception ex){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
isSuccess = false;
}
...
}
private boolean validate(){
...
}
}
Here is the test class ...
public class TestAuthorizeDotNetLib{
@isTest
private static void testclasses(){
DoPaymentHandler doPayment = new DoPaymentHandler();
doPayment.process();
doPayment.amount='test';
doPayment.ponumber='test';
doPayment.description='test';
doPayment.firstname='test';
doPayment.lastname='test';
doPayment.email='test';
doPayment.state='test';
doPayment.zip='test';
doPayment.cardnumber='test';
doPayment.expiration='test';
doPayment.process();
}
}
When running the test I get the error message "Problems with System.NullPointerException: Attempt to de-reference a null" on the line with "doPayment.process();"
Help is much appreciated.
Robert aka TheFrisco
It's failing probably because the reg inside the class is null, since you
didn't provide this data.
I think you can do it in 2 ways:
1.Provide the class this data
doPayment.reg = new Registration__c();
2.Test it by mimic the action of opening the page
Registration__c myReg=new Registration__c();
Test.setCurrentPageReference(new PageReference('Page.thePageName'));
ApexPages.StandardController sc = new ApexPages.standardController(myReg);
DoPaymentHandler contClass = new DoPaymentHandler(sc);
contClass.process();
Note that in either case, you should create the data - Registration__c.
When the text class running, it's start with empty tables, you need to create it before.
All Answers
It's failing probably because the reg inside the class is null, since you
didn't provide this data.
I think you can do it in 2 ways:
1.Provide the class this data
doPayment.reg = new Registration__c();
2.Test it by mimic the action of opening the page
Registration__c myReg=new Registration__c();
Test.setCurrentPageReference(new PageReference('Page.thePageName'));
ApexPages.StandardController sc = new ApexPages.standardController(myReg);
DoPaymentHandler contClass = new DoPaymentHandler(sc);
contClass.process();
Note that in either case, you should create the data - Registration__c.
When the text class running, it's start with empty tables, you need to create it before.
Thanks I used the second approach and that got me to where I could package my code. YOU ROCK!!!