+ Start a Discussion
ForceRookieForceRookie 

How to make test class of VF Controller that calls the batch?

Please help me, I can't see any reference on how to make test class for it..

public class ProcessController {
    public ProcessController(ApexPages.StandardSetController stdController) {}
    public PageReference runBatch() {
        MyUpdateBatch batch = new MyUpdateBatch ();
        Database.executeBatch(batch);
        
        return new ApexPages.Action('{!List}').invoke();
    }
}

Thanks!
AnudeepAnudeep (Salesforce Developers) 
Hi ForceRookie, 

I believe you will have to just try and invoke the runBatch Method

@isTest 
public class ProcessControllerTest
{
    static testMethod void testMethod1() {
        ProcessController pc = new ProcessController(); 
        pc.runBatch();
        Test.startTest();
        MyUpdateBatch batch = new MyUpdateBatch();
        Id batchId = Database.executeBatch(batch);
        Test.stopTest();
    }
    //Sample template for testing StandardSetController
    static testMethod void testMethod1() 
    {
        List <Account> lstAccount = new List<Account>();
        
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        lstAccount.add(testAccount);
        Account testAccount1 = new Account();
        testAccount1.Name='Test Account1' ;
        lstAccount.add(testAccount1);
        
        insert  lstAccount;
        
        Test.startTest();
        Test.setCurrentPage(Page.your_page);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount);
        stdSetController.setSelected(lstAccount);
        your_extension ext = new your_extension(stdSetController);
        Test.stopTest();
    }
}

Thank you
Anudeep