Skip to main content
I have a custom class which needs testing but I have no clue how to and where to get started. 

Below is an example of the original. 

This code is used on a custom Apex page to generate some output.

public with sharing class getCustomvalues {

private final Account account;

public List<String> myitems {get; private set;}

public List<Account_items__c> result {get;set;}

public getCustomvalues() {

Id id = ApexPages.currentPage().getParameters().get('id');

account = [SELECT Account.Name, items__c FROM Account WHERE Id = :id];

myitems = new List<String>();

if (account.items__c== null) {

/*

code

*/

} else {

/*

code

*/

}

}

public Account getAccount() {

return account;

}

}

My questions:

  1. How to instantiate this class from a testing class?
  2. How to set the currentPage Id ?

    1. Should I use a stub class too?
2 Antworten
  1. 14. Juli 2022, 09:16
    Hii RoleOfWal

    Try Below Test Class

    @isTest

    public class getCustomvaluesTest {

    @isTest

    public static void unitTest(){

    Account Acc= new Account();

    Acc.Name = 'Test';

    insert Acc;

    Account Acc2 = new Account();

    Acc2.Name = 'Account2';

    Acc2.ParentId = Acc.Id;

      Acc2.items__c = //Fill This

    Insert Acc2;

    ApexPages.currentPage().getParameters().put('id',Acc2.Id);

    getCustomvalues gcv = new getCustomvalues();

    gcv.getAccount();

    }

    }

    Please Mark It As Best Asnwer If It Helps

    Thank You!
0/9000