function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shubham Sinha 49Shubham Sinha 49 

How to writeTest class for Custom controller

Hi,
I am new to Salesforce . I need to write a test class for custom controller. 
Please help me
public class UpdateGoogleApiController {

    public string endpoint{get; set;}

    //Insert the new custom setting record in the database
    public void saveRecord(){
        CREDE__GoogleAPIEndPoint__c googleKey  = [SELECT Id, name from CREDE__GoogleAPIEndPoint__c][0];
       CREDE__GoogleAPIEndPoint__c gAPI  = new CREDE__GoogleAPIEndPoint__c();
        gAPI.Id = googleKey.Id; 
        gAPI.CREDE__EndpointURL__c = endpoint;
       
        Update gAPI;
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'API key value is updated Successfully.Thank you!'));
        //Redirect to new record detail page
      // return new PageReference('/' + googleKey.Id);
      // return null;
    }
}

 
Best Answer chosen by Shubham Sinha 49
FARSANA PSFARSANA PS
Hai Shubham,

Try this test class..
@istest
public class updateGoogleApiControllerTest {
     @testSetup static void setup() {
       CREDE__GoogleAPIEndPoint__c apiEndPoint=new CREDE__GoogleAPIEndPoint__c(name='google test');
        insert apiEndPoint;        
    }
    
     @isTest static void testsaveRecord() {
       UpdateGoogleApiController apiControllerInstance=new UpdateGoogleApiController();
         String endurl='https://www.google.com/';
         apiControllerInstance.endpoint=endurl;
           apiControllerInstance.saveRecord();
           CREDE__GoogleAPIEndPoint__c googleKey  = [SELECT Id, name,CREDE__EndpointURL__c from CREDE__GoogleAPIEndPoint__c limit 1];
        System.assertEquals(endurl,googleKey.CREDE__EndpointURL__c);
    }

}
Hope this helps...