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
Nishad BashaNishad Basha 

how write the test class for restservice?

@RestResource(urlMapping='/MyAccount12/*')
global with sharing class MyAccountRest {

    @HttpDelete
    global static void doDelete() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account account = [SELECT Id FROM Account WHERE Id = :accountId];
        delete account;
    }
  
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
        return result;
    }
  
  @HttpPost
    global static String doPost(String name,
        String phone, String website) {
        Account account = new Account();
        account.Name = name;
        account.phone = phone;
        account.website = website;
        insert account;
        return account.Id;
    }
}

above code how to write the test class so far.please give some ideas.
hitesh90hitesh90
Hello Nishad,

Please check below sample code for write the test class for REST webservice.

Test Class:
@isTest
private class MyAccountRestTest{
    static testMethod void testDelete() {

        Account objAcc = new Account();
        objAcc.Name = 'Test Account';
        insert objAcc;
        
        //do request
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();

        req.requestURI = '/services/apexrest/MyAccount12/' + objAcc.id;  
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;

        MyAccountRest.doDelete();
    }

    static testMethod void testGet() {
        
        Account objAcc = new Account();
        objAcc.Name = 'Test Account';
        insert objAcc;
        
        //do request
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();

        req.requestURI = '/services/apexrest/MyAccount12/' + objAcc.id;  
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;

        Account acc = MyAccountRest.doGet();
    }
    
    static testMethod void doPost() {
        
        //do POST request
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/MyAccount12/';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;

        String results = MyAccountRest.doPost('Test Account', '123456', 'http://www.test.com');
    }
}

Let me know if you have any question on this.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Nishad BashaNishad Basha
HI,Hitesh

 above test class is not working please give another code
hitesh90hitesh90
What is the issue in that test class?
can you please post error here?
Jai ChaturvediJai Chaturvedi
Hi,

Use this.
 
static testMethod void testInsertLead() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        insertUser();
        req.requestURI = '/servicerequest/apexrest/MyAccount12';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
//Use starTest, stoptest to make callout.
        Test.startTest();
            ClassName.doPost('20011100','xyz','11111','Insert','True');
        Test.StopTest();
    }

Use test.StartTest() and Test.StopTest() to make callout.