I am writing the test class for StandardSetController for Selected Records(Records are get Selected when SelectRecords from Checkbox using ListView). So In my test class, it is giving me an error of 'Attempt to De-reference a null object'. Help me into this
public with sharing class GlobalApprovalPageExtension {
public GlobalApprovalPageExtension(ApexPages.StandardController controller) {
}
private ApexPages.StandardSetController standardController;
public List<SCORE_Cost_Savings__c> ListSelectedScoreCost {get;set;}
public Integer TotalSubmittedValues {get;set;}
public GlobalApprovalPageExtension(ApexPages.StandardSetController standardController) {
this.standardController = standardController;
ListSelectedScoreCost = new List<SCORE_Cost_Savings__c>();
TotalSubmittedValues = [SELECT count() FROM SCORE_Cost_Savings__c WHERE Status__c='Regional Approved'];
system.debug('TotalSubmittedValues---------------->'+TotalSubmittedValues);
List<SCORE_Cost_Savings__c> selectedSubmitted = new List<SCORE_Cost_Savings__c>();
selectedSubmitted = (List<SCORE_Cost_Savings__c>) standardController.getSelected();
system.debug('set------->>'+selectedSubmitted);
SET<ID> SelectedID = new Set<ID>();
system.debug('selectedSubmitted------------->'+selectedSubmitted);
for(SCORE_Cost_Savings__c objScore:selectedSubmitted){
SelectedID.add(objScore.id);
}
system.debug('---------------->'+SelectedID);
ListSelectedScoreCost = [SELECT id,Name,Status__c FROM SCORE_Cost_Savings__c WHERE Status__c='Regional Approved' AND ID IN:SelectedID];
if(ListSelectedScoreCost.size()==0){
apexpages.addMessage(new Apexpages.Message(ApexPages.severity.Info, 'Kindly Select the Scores from List View'));
}
}
//This method is for Updating the Status of Score to Global Approved
public void UpdateStatus(){
try{
system.debug('ListUpdateStausScore----------->'+ListSelectedScoreCost);
List<SCORE_Cost_Savings__c> ListUpdateStausScore = new List<SCORE_Cost_Savings__c>();
for(SCORE_Cost_Savings__c objScore:ListSelectedScoreCost){
objScore.Status__c = 'Global Approved';
ListUpdateStausScore.add(objScore);
}
if(ListUpdateStausScore.size()>0){
Update ListUpdateStausScore;
apexpages.addMessage(new Apexpages.Message(ApexPages.severity.Confirm, 'Score Status Updated to Global Approved'));
}
}catch(Exception e){
apexpages.addMessage(new Apexpages.Message(ApexPages.severity.ERROR, 'Exception due to ----->'+e.getMessage()+e.getLineNumber()));
system.debug('Error Messages------------->'+e.getMessage()+e.getLineNumber());
}
}
}
Test Class
@isTest
public class GlobalApprovalPageExtensionTest {
//Global variables
private static SCORE_Cost_Savings__c objSavings;
public Integer TotalSubmittedValues;
private static SCORE_Cost_Savings__c objSaving;
private static List<SCORE_Cost_Savings__c> slist = new List<SCORE_Cost_Savings__c>();
private static List<SCORE_Cost_Savings__c> slist1 = new List<SCORE_Cost_Savings__c>();
//test data setup method
@testsetup public static void testData(){
objSavings = new SCORE_Cost_Savings__c(Code__c='224883',
Entry_Date__c=System.Today(),
Score_Date__c=System.Today(),
Event_Name__c='Test1',
Event_Start__c=System.Today(),
Negotiated_Cost_in_Local_Currency__c=800.00,
Country__c='Americas',
Currency_Type__c='AED',
Description_of_Cost_Savings__c='This is test',
Event_End__c=System.Today(),
Starting_Cost_in_Local_Currency__c=1000.00,
Status__c='Regional Approved'
);
insert objSavings;
objSaving = new SCORE_Cost_Savings__c(Code__c='224883',
Entry_Date__c=System.Today(),
Score_Date__c=System.Today(),
Event_Name__c='Test1',
Event_Start__c=System.Today(),
Negotiated_Cost_in_Local_Currency__c=800.00,
Country__c='Americas',
Currency_Type__c='AED',
Description_of_Cost_Savings__c='This is test',
Event_End__c=System.Today(),
Starting_Cost_in_Local_Currency__c=1000.00,
Status__c='Regional Approved');
insert objSaving;
}
//First method to validate the Extension class to Update the Status of Score to Regional Approved.
public static testmethod void validateGlobalApprovalPageExtensionOne(){
testData();
// Set Id of score cost saving to current page
//ApexPages.currentPage().getParameters().put('id', objSaving.Id);
//objSaving.setSelected(slist);
slist.add(objSaving);
//Instance of class
ApexPages.StandardSetController standardController = new ApexPages.StandardSetController(slist);
standardController.setSelected(slist);
standardController.getSelected();
ApexPages.StandardController sc = new ApexPages.StandardController(objSaving);
GlobalApprovalPageExtension objGlobalApprovalPageExtension = new GlobalApprovalPageExtension(sc);
GlobalApprovalPageExtension objGlobalApproval = new GlobalApprovalPageExtension(standardController);
objGlobalApprovalPageExtension.UpdateStatus();
//System.assertEquals('Global Approved',objSavings.Status__c);
}
//Second method to validate the Extension class to Update the Status of Score to Regional Approved.
public static testMethod void validateRegionalApprovalPageExtensionTwo(){
testData();
// Set Id of score cost saving to current page
//ApexPages.currentPage().getParameters().put('id', objSavings.Id);
//objSaving.setSelected(slist);
slist.add(objSavings);
//Instance of class
ApexPages.StandardSetController standardController = new ApexPages.StandardSetController(slist);
standardController.setSelected(slist);
ApexPages.StandardController sc = new ApexPages.StandardController(objSavings);
GlobalApprovalPageExtension objGlobalApprovalPageExtension = new GlobalApprovalPageExtension(sc);
GlobalApprovalPageExtension objGlobalApproval = new GlobalApprovalPageExtension(standardController);
standardController.getSelected();
objGlobalApprovalPageExtension.UpdateStatus();
//objSaving.Status__c='Global Approved';
//System.assertEquals('Global Approved',objSavings.Status__c);
}
}