function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Egor PalatovEgor Palatov 

Remove button test class

Can someone help me to write test class for this method?(shownow is a lise of my objects that current displayed on my VF page
public void removeDetail(){
	Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));
	shownow.remove(indexVal - 1);            
}

 
Best Answer chosen by Egor Palatov
Waqar Hussain SFWaqar Hussain SF
Assuming you Class Name is Class_Name 
And you page name is Page_Name
Class_Name cls = new Class_Name ();
Test.setCurrentPageReference(new PageReference('Page.Page_Name'));
System.currentPageReference().getParameters().put('index', '5');
cls.removeDetail();

 

All Answers

Waqar Hussain SFWaqar Hussain SF
Assuming you Class Name is Class_Name 
And you page name is Page_Name
Class_Name cls = new Class_Name ();
Test.setCurrentPageReference(new PageReference('Page.Page_Name'));
System.currentPageReference().getParameters().put('index', '5');
cls.removeDetail();

 
This was selected as the best answer
Egor PalatovEgor Palatov
Hi Waqar, this test fails with  "System.ListException: List index out of bounds: 4" but it cover the code, can i somehow pass this test?
Waqar Hussain SFWaqar Hussain SF
Class_Name cls = new Class_Name ();
Test.setCurrentPageReference(new PageReference('Page.Page_Name'));
System.currentPageReference().getParameters().put('index', '1');
cls.removeDetail();

try using above code snippet.