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
Ido Greenbaum 10Ido Greenbaum 10 

Lightning Case List View custom button - assistance with tests

Can anyone please help with a test class for the following Lightning Case List View custom 'take ownership' button? : 
 
public class CaseAcceptOwnership {
    PageReference cancel;
    Case[] records;
    public CaseAcceptOwnership(ApexPages.StandardSetController c) {
        records = (Case[])c.getSelected();
        cancel = c.cancel();
    }
    public PageReference updateCases() {
        for(Case record: records) {
            record.OwnerId = UserInfo.getUserId();
        }
        update records;
        return cancel.setRedirect(true);
    }
}

 
Best Answer chosen by Ido Greenbaum 10
Hemant_SoniHemant_Soni
Hi Ido,
Try this code,
@isTest 
public class CaseAcceptOwnershipTest 
{
 static testMethod void testMethod1() 
 {
 List <Case> lstCase = new List<Case>();
 
 Case oCase = new Case();
 oCase.Subject='Test Case' ;
 lstCase.add(oCase);
 Case oCase1 = new Case();
 oCase1.Name='Test Account1' ;
 oCase1.add(oCase1);

 insert  lstCase;
 
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstCase);
  stdSetController.setSelected(lstCase);
  CaseAcceptOwnership  oCaseAccOwner = new CaseAcceptOwnershipstdSetController);
		oCaseAccOwner.updateCases();
 Test.stopTest();
 }
}
Hope this will help you.Please make is solved.
Thanks
Hemant