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
si risi ri 

how to call global static void methods in test class

Hi guys,
I knew how to calll void methods in test but How to call global static void  Rest API methods in test class?
Khan AnasKhan Anas (Salesforce Developers) 

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
global class MyClass{

    global static void updateMethod(){    
        try {        
            //get the json from the request
            RestRequest req1=RestContext.request;           
            String jsonInput= req1.requestBody.toString();       

            //create map for the obtained json input
            Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(jsonInput); 
            Id opptyId = String.valueOf(jsonMap.get('opportunity_SF_Id')) ;            
            //get the opportunity object       
            Sobject oppty = [SELECT id, description FROM opportunity WHERE id=:opptyId];
            //some more processing
            update oppty;        
        }catch(Exception e){
               System.debug('The following exception has occurred: ' + e.getMessage());

Test Class:
private class MyClassTest
    static testMethod void MyClassMethod()
        Account acc = new Account();
        acc.AccountNumber ='12345';
        insert acc;
		Opportunity mAWSUsage = new Opportunity( Name = 'mAWS Usage',
							  AccountId = acc.Id,
							  StageName = 'Negotiations',
							  CloseDate =,
							  Type = 'New Business - Add',
							  Amount = 555888555
		insert  mAWSUsage;
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/abc/xyz/';
        request.httpMethod = 'POST';
        RestContext.request = request;
        //System.assert(acc != null);

Please refer to the below links which might help you further with the above requirement.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas