-1

This question already has an answer here:

How to cover this code? enter image description here

enter image description here Test class Code

     @isTest static void getPageNumberListTest(){
    Controller controller = new Controller();  
    controller.TotalPages=10;
    controller.getPageNumberList();

}

Conroller Class

public Integer TotalPages {
get {   
    if (setList.getResultSize() <= 10)
        this.TotalPages = 1;
    if (Math.Mod ( setList.getResultSize(), setList.getPageSize() ) == 0)
        this.TotalPages = ( setList.getResultSize() / setList.getPageSize() );
    if (Math.Mod ( setList.getResultSize(), setList.getPageSize() ) != 0)
     this.TotalPages = ( setList.getResultSize() / setList.getPageSize() )+1;
    return TotalPages;
}
set; 

}

public Integer TotalRecords {
    get { 
        this.TotalRecords = setList.getResultSize();
        return TotalRecords;
    }
    set;
}
public ApexPages.StandardSetController setList {
get {
    if (setList == null) { 
        setList = new ApexPages.StandardSetController(Database.getQueryLocator(
        [SELECT  Name, Email, Contact_Level__c, AccountID, OwnerID, CreatedByID, CreatedDate FROM Contact]));
    }
    if (this.PageSize == null) PageSize = 10; 
    setList.setPageSize(PageSize); 


    return setList;
} set;

} enter image description here

marked as duplicate by Pranay Jaiswal, glls, Mohith Shrivastava apex Aug 4 at 15:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Does TotalPages or TotalRecords have a getter or setter? If so, please edit to include them. – David Reed Aug 2 at 11:48
  • your testmethod needs to create Contacts; – cropredy Aug 2 at 22:14
0

I would suggest to create a 11 records and pass them into standard controller constructor.

Then pass standard controller into custom controller constructor and assert that TotalPages is 2 and TotalRecords is 11.

Also create another test method for which create 10 records and pass them into standard controller constructor.

Then pass standard controller into custom controller constructor and assert that TotalPages is 1 and TotalRecords is 10.

These two test methods would give the full coverage

0

You can use the below mentioned way:

ApexPages.StandardController standardcontroller = new ApexPages.StandardController(sObjectRecord);
ConrollerClass classInstance = new ConrollerClass(standardcontroller);
classInstance.TotalPages();
classinstance.getPageNumberList();
  • Variable does not exist: sObjectRecord – Ihar Sharel Aug 2 at 17:12
  • You need to include the sObject you are working with. Here I could see that iT is Contact. So, get a Contact record and pass the reference in place of sObject. – Shahroz Beig Aug 3 at 3:56
  • Contact con = new Contact(FirstName='sadsa',LastName ='dasdsa',Email='dsadas@mail.ru',Contact_Level__c='Primary'); ApexPages.StandardController standardcontroller = new ApexPages.StandardController(con); Controller controller = new Controller(standardcontroller); – Ihar Sharel Aug 6 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.