You need to sign in to do that
Don't have an account?
Sailaja N 4
Test class error: System.NullPointerException: Argument cannot be null.
Hi everypone,
Please help me on fixing this error in my test class
@isTest
private class ItemizedReceiptTest {
static testMethod void testMethodPostive(){
Product2 prod = new Product2(
Name = 'Test',
IsActive = true
);
insert prod;
Account acc = new Account(Name ='Test');
insert acc;
//Create your product
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.AccountId=acc.id;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Id pricebookId = Test.getStandardPricebookId();
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
//create your opportunity line item. This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = testOpp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = pbEntry.UnitPrice
);
insert oli;
PageReference pageRef = Page.ItemizedReceipt;
Test.setCurrentPage(pageRef);
system.debug('Products'+prod);
system.debug('acc'+acc);
system.debug('testOpp'+testOpp);
system.debug('pbEntry'+pbEntry);
system.debug('oli'+oli);
ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
ItemizedReceipt obj = new ItemizedReceipt(sc);
obj.total();
}
}
class:
public with sharing class ItemizedReceipt {
public List<OpportunityLineItem> oppLst { set; get; } //List of Products in a Opportunity
public Double totalPrice_cal { set; get; } //Variable to calculate Total Price of all the products
public Double totalGST_cal { set; get; } //Variable to calculate GST of all the products
public Double totalPST_cal { set; get; } //Variable to calculate PST of all the products
public Double totalAmt_cal { set; get; } //Variable to calculate Total Amount of all the products
public DateTime getCloseDate { set; get; } //Variable to get the Created Date
public String dayOfWeek { set; get; } //Variable to store the Day from the Created Date
public String accName { set; get; } //Variable to store the Customer Name
public ItemizedReceipt(ApexPages.StandardController controller) {
if ( controller.getId() != null ) {
oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
Total_w_Tax__c, Opportunity.Account.Name
FROM OpportunityLineItem
WHERE Opportunity.Id=:controller.getId()
];
getCloseDate = oppLst[0].Opportunity.CloseDate;
dayOfWeek = getCloseDate.format('EEEE');
accName = (string)oppLst[0].Opportunity.Account.Name;
totalPrice_cal = 0.0;
totalGST_cal = 0.0;
totalPST_cal = 0.0;
totalAmt_cal = 0.0;
total();
}
}
//Method calculates all the totals
public void total() {
for ( integer i = 0; i < oppLst.size(); i++ ) {
totalPrice_cal += oppLst[i].TotalPrice;
totalGST_cal += oppLst[i].GST__c;
totalPST_cal += oppLst[i].PST__c;
totalAmt_cal += oppLst[i].Total_w_Tax__c;
}
}
}
Thank You
Please help me on fixing this error in my test class
@isTest
private class ItemizedReceiptTest {
static testMethod void testMethodPostive(){
Product2 prod = new Product2(
Name = 'Test',
IsActive = true
);
insert prod;
Account acc = new Account(Name ='Test');
insert acc;
//Create your product
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.AccountId=acc.id;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Id pricebookId = Test.getStandardPricebookId();
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
//create your opportunity line item. This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = testOpp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = pbEntry.UnitPrice
);
insert oli;
PageReference pageRef = Page.ItemizedReceipt;
Test.setCurrentPage(pageRef);
system.debug('Products'+prod);
system.debug('acc'+acc);
system.debug('testOpp'+testOpp);
system.debug('pbEntry'+pbEntry);
system.debug('oli'+oli);
ApexPages.StandardController sc = new ApexPages.StandardController(testOpp);
ItemizedReceipt obj = new ItemizedReceipt(sc);
obj.total();
}
}
class:
public with sharing class ItemizedReceipt {
public List<OpportunityLineItem> oppLst { set; get; } //List of Products in a Opportunity
public Double totalPrice_cal { set; get; } //Variable to calculate Total Price of all the products
public Double totalGST_cal { set; get; } //Variable to calculate GST of all the products
public Double totalPST_cal { set; get; } //Variable to calculate PST of all the products
public Double totalAmt_cal { set; get; } //Variable to calculate Total Amount of all the products
public DateTime getCloseDate { set; get; } //Variable to get the Created Date
public String dayOfWeek { set; get; } //Variable to store the Day from the Created Date
public String accName { set; get; } //Variable to store the Customer Name
public ItemizedReceipt(ApexPages.StandardController controller) {
if ( controller.getId() != null ) {
oppLst = [SELECT Product2.Name, Opportunity.Id, Opportunity.CloseDate, TotalPrice, GST__c, PST__c,
Total_w_Tax__c, Opportunity.Account.Name
FROM OpportunityLineItem
WHERE Opportunity.Id=:controller.getId()
];
getCloseDate = oppLst[0].Opportunity.CloseDate;
dayOfWeek = getCloseDate.format('EEEE');
accName = (string)oppLst[0].Opportunity.Account.Name;
totalPrice_cal = 0.0;
totalGST_cal = 0.0;
totalPST_cal = 0.0;
totalAmt_cal = 0.0;
total();
}
}
//Method calculates all the totals
public void total() {
for ( integer i = 0; i < oppLst.size(); i++ ) {
totalPrice_cal += oppLst[i].TotalPrice;
totalGST_cal += oppLst[i].GST__c;
totalPST_cal += oppLst[i].PST__c;
totalAmt_cal += oppLst[i].Total_w_Tax__c;
}
}
}
Thank You
I have understand your question.
You are not inserted GST/PST/Total_W_Tax field data from test class that will return null that is the reason it is given argument null.
I have excecuted test class and apex class in my org. it run successfully and also added comments in the code have a look at that.
Please let us know if it helps you!
Thanks & regards
Madhukar_Heptarc
But GST ,PST values are calculated in workflow,
Total_W_Tax field is a Formula field.
if i am commenting GST ,PST values class is running succesfully
It seems like you dont have to update GST and PST, since these fields will be updated from workflow.
What is condition to invoke workflow ?
Is this resolved ?