+ Start a Discussion
anshiSFDCanshiSFDC 

class and test class

Hi everyone,the below is the class which i have written,tried testclass for that class is that crct way of writing testclass for that class.
class:
public class opp_paginationcontroller {
    Public Integer size{get;set;} 
    Public Integer noOfRecords{get; set;} 
    public List<SelectOption> paginationSizeOptions{get;set;}
         
    public opp_paginationcontroller(){
        size=10;
        paginationSizeOptions = new List<SelectOption>();
        paginationSizeOptions.add(new SelectOption('5','5'));
        paginationSizeOptions.add(new SelectOption('10','10'));
        paginationSizeOptions.add(new SelectOption('20','20'));
        paginationSizeOptions.add(new SelectOption('50','50'));
        paginationSizeOptions.add(new SelectOption('100','100'));
    }
     
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select id,Name,AccountId,Account.name,Amount,StageName,CloseDate,LastModifiedDate from Opportunity]));
                setCon.setPageSize(size);  
                noOfRecords = setCon.getResultSize();
            }            
            return setCon;
        }
        set;
    }
     
    // It Changes the size of pagination
    public PageReference refreshPageSize() {
         setCon.setPageSize(size);
         return null;
    }
 
    // Initialize setCon and return a list of record    
     
    public List<Opportunity> getOpportunities() {
         return (List<Opportunity>) setCon.getRecords();
    }
}
testclass:
@isTest
public class Testopp_paginationcontroller {
@isTest
    static void invoke(){
       opportunity o=new opportunity();
        o.name='testopp';
        o.stageName='prospecting';
        o.closeDate=system.today();
        insert o;
        Test.startTest();
        opp_paginationcontroller opp=new opp_paginationcontroller();
       
        opp.refreshPageSize();
        opp.getOpportunities();
        Test.stopTest();
    }
}
veer soniveer soni
hy,
try below test class with 100% code coverage.
@isTest
public class opp_paginationcontrollerTest {
@isTest
    private static void test_Unit(){
        
         Account a = new Account();
        a.Name = 'Test Account';
        insert a;
        system.assertEquals('Test Account', a.Name);
           Opportunity o = new Opportunity();
        o.Name = 'Test Opportunity';
        o.AccountId = a.Id;
        o.StageName = 'Prospecting';
        o.CloseDate = system.today() + 1;
        insert o;
        opp_paginationcontroller OppPagination = new opp_paginationcontroller();
        OppPagination.getOpportunities();
        OppPagination.refreshPageSize();
         system.assertEquals('Prospecting', o.StageName);
        
    }
}

don't forget to mark it as best answer if it helps you.
Thank you
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Your code is right. Please tell me what errors are you getting.

@isTest
public class Testopp_paginationcontroller {
@isTest
   public static void oppTest(){
       opportunity o=new opportunity();
        o.name='testopp';
        o.stageName='prospecting';
        o.closeDate=system.today();
        insert o;
        Test.startTest();
        opp_paginationcontroller opp=new opp_paginationcontroller();
       
        opp.refreshPageSize();
        opp.getOpportunities();
        Test.stopTest();
    }
}
Thank You
anshiSFDCanshiSFDC
Hi , thanks for your reply What I wrote the test class am not getting any errors it is 100 %,but am asking what I wrote is crct way of writing the test class or not. Thanks