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
Henrique RodriguesHenrique Rodrigues 

How to develop a test class for Rest webservice

Hello everyone, i'm developing a webservice in rest and i have no ideia about how write a test class in this case and this is my code
@RestResource(urlMapping='/upsertaccount')
global with sharing class WSCustomerEdit {
   
   @HttpPost
    global static Account doPost(){
        String accountJSON = RestContext.request.requestBody.toString();
        JsonAccount serializedAcc = (JsonAccount)(JSON.deserialize(accountJSON, JsonAccount.class));
    
        Account acc = [SELECT Id FROM Account WHERE id =: serializedAcc.Id];
        acc.Name = serializedAcc.name;
        acc.Phone = serializedAcc.phone;
        upsert acc;
        return acc;
        
    }

    global class JsonAccount{
        public String id      {get;set;}
        public String name    {get;set;}
        public String phone   {get;set;}
        public String address {get;set;}
    }
}
and my test class
@isTest
public class CustomerEditTest {

  @isTest
	static void teste(){
        
		WSCustomerEdit.JsonAccount acc = new WSCustomerEdit.JsonAccount();
        
		acc.id      = 'test';
		acc.name    = 'NameTest';
		acc.phone   = '1198785496';
		acc.address = 'Rua x';

		String myJSON = JSON.serialize(acc);
	}
}

how can I make a test class with 100% of code coverage? 
 
Best Answer chosen by Henrique Rodrigues
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class CustomerEditTest {

  @isTest
	static void teste(){
        
		Account acc1= new Account();
		acc1.Name ='Test';
		insert acc1;
		
		WSCustomerEdit.JsonAccount acc = new WSCustomerEdit.JsonAccount();
		acc.id      = acc1.id;
		acc.name    = 'NameTest';
		acc.phone   = '1198785496';
		acc.address = 'Rua x';
		String myJSON = JSON.serialize(acc);
		

        RestRequest request = new RestRequest();
        request.requestUri ='https://cs13.salesforce.com/services/apexrest/upsertaccount';
        request.httpMethod = 'POST';
		request.requestBody = Blob.valueof(myJSON);
		
        RestContext.request = request;
		Account accObj = WSCustomerEdit.doPost();
		
		
	}
}

Let us know if this will help you
 

All Answers

Aslam ChaudharyAslam Chaudhary
Try Below code 
HttpRequest req = new HttpRequest();
		req.setMethod('POST');
		req.setHeader('content-type', 'application/json');
		req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId() );
		req.setEndpoint("https://cs13.salesforce.com/services/apexrest/upsertaccount' );
		req.setBody( myJSON );
       
		Http http = new Http();
		HTTPResponse resp = http.send( req );
		myresponse = resp.getBody();
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class CustomerEditTest {

  @isTest
	static void teste(){
        
		Account acc1= new Account();
		acc1.Name ='Test';
		insert acc1;
		
		WSCustomerEdit.JsonAccount acc = new WSCustomerEdit.JsonAccount();
		acc.id      = acc1.id;
		acc.name    = 'NameTest';
		acc.phone   = '1198785496';
		acc.address = 'Rua x';
		String myJSON = JSON.serialize(acc);
		

        RestRequest request = new RestRequest();
        request.requestUri ='https://cs13.salesforce.com/services/apexrest/upsertaccount';
        request.httpMethod = 'POST';
		request.requestBody = Blob.valueof(myJSON);
		
        RestContext.request = request;
		Account accObj = WSCustomerEdit.doPost();
		
		
	}
}

Let us know if this will help you
 
This was selected as the best answer
{tushar-sharma}{tushar-sharma}
First, create the Test data, and then you can use a similar code to test it.
//Test POST API
        req.requestURI = '/services/apexrest/Account';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        RestContext.request = req;
        RestContext.response= res;
        
        String recId = MyRestResource.dopost('Test','9887988798','https://newstechnologystuff.com');

https://newstechnologystuff.com/2020/05/27/test-salesforce-rest-api-using-postman/

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/