+ Start a Discussion
Ruta GalanskaRuta Galanska 

VF StandartSetController Test class - Next()&&Previous() Exception, ApexMessages.

Hallo,
I wonder is it possible to cover Next() && Previous() Exception- Apex Messages in VF StandartSetController Test Class?
I dont think that rest of code is necessary for getting the idea, but if it's let me know.
The Idea of exceptions in Next/Previous is that it don't allow switch between VF pages before records (inline edit) is saved or canceled.

Do I have to create sepreate test method for pagination, do it right in existing method? Is there any way how i can create the exception situation? It's confusing as method tests are based on VFController SetController where is also SelectOption page size..and Next/Previous methods are defaults for class.

public Boolean hasNext{
        get{
            return setCon.getHasNext();
        }   
        set;
    }
   
    public void next() {  
        try{
        setCon.next();
        	} catch (Exception e) {
           		 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'Please Save or Cancel your changes in records. ERROR: ' + e.getMessage());

            	ApexPages.addMessage(myMsg);          
        	}
        }

    public Boolean hasPrevious  {
        get  {
            return setCon.getHasPrevious();
        } 
        set;
    }     
     
    public void previous() {   
        try{
        setCon.previous();
        } catch (Exception e){
           ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'Please Save or Cancel your changes in records' + e.getMessage());

            ApexPages.addMessage(myMsg);    
        }
    }

Here is the Test Method. It covers everything except Next()&&Previous() Exception Apex Messages.
@isTest 
    
    @testSetup static void TestData(){  
        
        Account acct = new Account(name ='Test');
        insert acct;
        
        List <Contact> cnList = new List <Contact>();
        for (Integer i = 0; i<5; i++){
            cnList.add( new Contact(AccountId=acct.id, LastName= 'Test cnt'+ i));
        }
        insert cnList;   
    }

public static void testVfPage(){
        Account acc = [Select ID, Name From Account Where Name='Test'];
        
        List<Contact> query=[SELECT Id, AccountId, LastName
                             FROM Contact 
                             WHERE AccountId = :acc.id
                             LIMIT 6];
        System.assertEquals(5, query.size());
        //There is 5 contacts inserted, put limit 6 for purpose
        
        Test.startTest();
        PageReference pr = Page.VfPage;
        Test.setCurrentPage(pr);
        ApexPages.currentPage().getParameters().put('id', String.valueOf(acc.Id));
        
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(query);
        stdSetController.setSelected(query);
        
       VfPageController t = new VfPageController();
        t.getContacts();
        
        //Buttons
        t.CustomCancel();
        t.CustomSave();          
        
        //Pages
        t.getTotalPages();       
        t.previous();
        t.next();
        t.refreshPageSize();
        
        Boolean test1 = t.hasNext;
        Boolean test2 = t.hasPrevious;
        Integer test3 = t.pageNumber;  
       Test.stopTest();
    }