You need to sign in to do that
Don't have an account?

Test Classes for apex facing error
public with sharing class expensesLineItem{
public final Expense_Line_Item__c el;
public expensesLineItem(ApexPages.StandardController stdController){
this.el = (Expense_Line_Item__c)stdController.getRecord();
}
public PageReference Dismiss(){
el.Display_Alert__c=false;
update el;
PageReference page = ApexPages.currentPage();
page.setRedirect(true);
return page;
}
}
Test class:
@isTest
public class expensesLineItem_Test{
@testSetup
static void setupTestData()
{
Expense__c exp = new Expense__c();
exp.Period_From__c = system.today();
exp.Period_To__c =system.today();
insert exp;
System.assertNotEquals(null, exp.Id);
Expense_Line_Item__c expLine = new Expense_Line_Item__c();
expLine.Name = exp.Id;
expLine.Expense_Head__c='Food' ;
expLine.Cost_Head__c= 'Campaign' ;
expLine.Amount__c= 10000;
System.assertNotEquals(null, expLine.Id);
insert expLine;
}
@isTest
static void testCall()
{
Expense_Line_Item__c expLine = [SELECT Id,Name,Amount__c,Bill_Available__c,Cost_Head__c,Expense_Head__c,Expense__c from Expense_Line_Item__c][0];
// System.assertEquals(true,expense_line_item_Obj.size()>0);
// List<Expense__c> expense_Obj = [SELECT Id from Expense__c];
//System.assertEquals(true,expense_Obj.size()>0);
ApexPages.StandardController stdCon = new ApexPages.StandardController(expLine);
expensesLineItem obj01 = new expensesLineItem(stdCon);
//expensesLineItem obj01 = new expensesLineItem(new ApexPages.StandardController(expense_line_item_Obj[0]));
obj01.Dismiss();
}
}
public final Expense_Line_Item__c el;
public expensesLineItem(ApexPages.StandardController stdController){
this.el = (Expense_Line_Item__c)stdController.getRecord();
}
public PageReference Dismiss(){
el.Display_Alert__c=false;
update el;
PageReference page = ApexPages.currentPage();
page.setRedirect(true);
return page;
}
}
Test class:
@isTest
public class expensesLineItem_Test{
@testSetup
static void setupTestData()
{
Expense__c exp = new Expense__c();
exp.Period_From__c = system.today();
exp.Period_To__c =system.today();
insert exp;
System.assertNotEquals(null, exp.Id);
Expense_Line_Item__c expLine = new Expense_Line_Item__c();
expLine.Name = exp.Id;
expLine.Expense_Head__c='Food' ;
expLine.Cost_Head__c= 'Campaign' ;
expLine.Amount__c= 10000;
System.assertNotEquals(null, expLine.Id);
insert expLine;
}
@isTest
static void testCall()
{
Expense_Line_Item__c expLine = [SELECT Id,Name,Amount__c,Bill_Available__c,Cost_Head__c,Expense_Head__c,Expense__c from Expense_Line_Item__c][0];
// System.assertEquals(true,expense_line_item_Obj.size()>0);
// List<Expense__c> expense_Obj = [SELECT Id from Expense__c];
//System.assertEquals(true,expense_Obj.size()>0);
ApexPages.StandardController stdCon = new ApexPages.StandardController(expLine);
expensesLineItem obj01 = new expensesLineItem(stdCon);
//expensesLineItem obj01 = new expensesLineItem(new ApexPages.StandardController(expense_line_item_Obj[0]));
obj01.Dismiss();
}
}
This is because of assert statement should after insert (I also missed that part):
This will work.
Thanks
Gulshan Raj
All Answers
I think you are missing to fill Expense__c for Expense_Line_Item__c in setupTestData.
I have changed your code to point the correct lookup (or master) relation value:
Please let me know if you have any question.
Thanks & Regards
Gulshan Raj
LinkedIn (https://www.linkedin.com/in/gulshan-raj-a26b0640/)
Twitter (https://twitter.com/gulshan_bittoo)
Still Its failing , It seems like your code is completely same as mine .I could not got where I had done mistake . Please review it if possible.
Thank you
Let us know if this will help you
Please try to update your code like below
The only change which you have to do is in:
expLine.Expense__c = exp.Id;
Replace your setupTestData method with this
If still facing issue, please provide error message. I will provide you fix.
Thanks
Gulshan Raj
Error MessageSystem.AssertException: Assertion Failed: Same value: null
Stack TraceClass.expensesLineItem_Test.setupTestData: line 18, column 1
this is the error message
This is because of assert statement should after insert (I also missed that part):
This will work.
Thanks
Gulshan Raj