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
SFDC12SFDC12 

Integration testclass

Hi everyone, can someonehelp how to write testclass for the below class

@RestResource(urlmapping='/v1/Accountmanager/')
global class Integration_Example {
@HttpGet
    //which is used to get the records or query the records
    global static Account doget(){
        //intilaise the object
       Account a=new Account();
        //get the req parameters in map
        map<string,string> paramsmap=Restcontext.request.params;
        //get the id
        string accid=paramsmap.get('Id');
        //query the records
        a=[select id ,name from Account where id=:accid];
        return a;
    }
    @HttpDelete
    global static Account doDelete(){
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE AccountNumber = :AccNumber ];
        delete result;
        return result;
    }
   @Httppost
    //post method is used to create the record
    global static Account docreate(string name){
        Account a = new Account(name=name);
        insert a;
        return a;
    }
     @Httpput
    global static Account doupdate(string name){
        map<string,string> paramsmap=Restcontext.request.params;
        //get the id
        string accid=paramsmap.get('Id'); 
        Account a = new Account(name=name, id=accid);
        update a;
        return a;
    }
}

Thanks in Advance
Best Answer chosen by SFDC12
Maharajan CMaharajan C
Hi Anusha,

Please try the below code:
 
@isTest
public class Integration_ExampleTest {
    public testmethod static void dogetTest(){
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'Get';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doget();
        Test.stopTest();
    }
    
    public testmethod static void doDeleteTest(){
        Account acc = new Account(Name = 'Test Account',AccountNumber = '1234', Phone = '1234567890', Website = 'Test@test.com');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'Get';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/1234' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doDelete();
        Test.stopTest();
    }
    
    public testmethod static void doPostTest(){
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.httpMethod = 'Post';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/name=test' ;  
        RestContext.request = req;
        RestContext.response = res;
        Test.startTest();
        Account a = Integration_Example.docreate('Test account');
        Test.stopTest();
    }
    
    public testmethod static void doupdateTest(){
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'PUT';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doupdate('Test Account');
        Test.stopTest();
    }
    
}


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Anusha,

Please try the below code:
 
@isTest
public class Integration_ExampleTest {
    public testmethod static void dogetTest(){
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'Get';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doget();
        Test.stopTest();
    }
    
    public testmethod static void doDeleteTest(){
        Account acc = new Account(Name = 'Test Account',AccountNumber = '1234', Phone = '1234567890', Website = 'Test@test.com');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'Get';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/1234' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doDelete();
        Test.stopTest();
    }
    
    public testmethod static void doPostTest(){
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.httpMethod = 'Post';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager/name=test' ;  
        RestContext.request = req;
        RestContext.response = res;
        Test.startTest();
        Account a = Integration_Example.docreate('Test account');
        Test.stopTest();
    }
    
    public testmethod static void doupdateTest(){
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Test.startTest();
        RestResponse res = new RestResponse();
        RestRequest req = new RestRequest();
        req.params.put('Id', acc.Id);
        req.httpMethod = 'PUT';
        req.addHeader('Content-Type', 'application/json'); 
        req.requestURI = '/services/apexrest/v1/Accountmanager' ;  
        RestContext.request = req;
        RestContext.response = res;
        Account a = Integration_Example.doupdate('Test Account');
        Test.stopTest();
    }
    
}


Thanks,
Maharajan.C
This was selected as the best answer
SFDC12SFDC12
Thanks a lot maharajan.
Maharajan CMaharajan C

Hi Anusha, Can you please mark the best answer !!!
SFDC12SFDC12
yeah sure
Daniel ShaneDaniel Shane
Thanks a lot for this. (https://www.quickpayportal.onl/)
Gladys J. BostickGladys J. Bostick
I want change in my dachser elogistics tracking (https://letracking.com/dachser-tracking/) api. Can anyone please help me?