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
Pedro SallesPedro Salles 

How to test an Apex class that uses @HttpGet

Hi,

I have a Get method that receives data through a URL:
 
@RestResource(urlMapping='/Contract/*')
global  class ContractREST {
    
    @HttpGet
    global static String getContratos() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String ContractId= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

        Contract result =  [SELECT Id , Regra_Separacao_Cartao__c, Origem_Contrato__c   
                                      from Contract where Id =: ContractId ];       

.
.
.
        JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStartObject();             
        
        gen.writeStringField('Id',result.Id);

        gen.writeEndObject();
        
        String jsonS = gen.getAsString();
             
        return contractJSON;
    }
  
}

I need to create a test class for my class, however I can not pass any parameters to the method because using GET does not accept.

Could someone help me with this question?

Thanks
Best Answer chosen by Pedro Salles
Raj VakatiRaj Vakati
Try this code .. Change your end point URL in the test class and make surre its not hard coded 
 
@isTest
public class ContractRESTTest {

  @isTest
	static void teste(){
        
		 Account AA = New Account();
      AA.Name = 'Test';
     
      AA.Type = 'Customer';
      AA.BillingStreet = 'Gatan 1';
      AA.BillingCity = 'Stockholm';
      AA.BillingCountry = 'SWE';
      AA.Customer_Type__c = 'A';
      Insert AA;
      
      Opportunity O = New Opportunity();
      O.Name = 'Test - KalleAnka';
      O.Type = 'New Buisness';
      O.Account = AA.Id;
      O.CloseDate = Date.today();
      O.StageName = 'Agreement';
      O.CurrencyIsoCode = 'SEK';
      O.ForecastCategoryName = 'Pipeline';
      Insert O;
      
      Contact Con = New Contact();
      Con.Account = AA;
      Con.LastName = 'Carlsson';
      Con.Position__c = 'Consultant';
      Con.CurrencyIsoCode = 'SEK';
      Insert Con;
      
      Contract C = New Contract();
      
      C.Account = AA;
      C.Status = 'Draft';
      C.Opportunity__r = O;
      C.CustomerSigned = Con;
      C.Product__c = 'B2B Analysis';
      C.No_of_users__c = 5;
      C.CurrencyIsoCode = 'SEK';
      C.Monthly_Subscription__c = 100;
      C.Total_Subscription_Amount__c = 1000;
      C.One_Off_Amount__c = 0;
      C.First_Contract_Start_Date__c = Date.today();
      C.StartDate = Date.today();
      C.ContractTerm = 2;
      C.Running_Subscription__c = 'No';
    Insert C;
	
      C.Status = 'Activated';
      Update C;
      
      
		

        RestRequest request = new RestRequest();
        request.requestUri ='https://cs13.salesforce.com/services/apexrest/upsertaccount';
        request.httpMethod = 'POST';
		 String jsonBB = '{"make":" ' +C.Id +'","year":"2020"}' ; 
		request.requestBody =jsonBB ;
		
        RestContext.request = request;
		 ContractREST.doPost();
		
		
	}
}

 

All Answers

Raj VakatiRaj Vakati
Try this code .. Change your end point URL in the test class and make surre its not hard coded 
 
@isTest
public class ContractRESTTest {

  @isTest
	static void teste(){
        
		 Account AA = New Account();
      AA.Name = 'Test';
     
      AA.Type = 'Customer';
      AA.BillingStreet = 'Gatan 1';
      AA.BillingCity = 'Stockholm';
      AA.BillingCountry = 'SWE';
      AA.Customer_Type__c = 'A';
      Insert AA;
      
      Opportunity O = New Opportunity();
      O.Name = 'Test - KalleAnka';
      O.Type = 'New Buisness';
      O.Account = AA.Id;
      O.CloseDate = Date.today();
      O.StageName = 'Agreement';
      O.CurrencyIsoCode = 'SEK';
      O.ForecastCategoryName = 'Pipeline';
      Insert O;
      
      Contact Con = New Contact();
      Con.Account = AA;
      Con.LastName = 'Carlsson';
      Con.Position__c = 'Consultant';
      Con.CurrencyIsoCode = 'SEK';
      Insert Con;
      
      Contract C = New Contract();
      
      C.Account = AA;
      C.Status = 'Draft';
      C.Opportunity__r = O;
      C.CustomerSigned = Con;
      C.Product__c = 'B2B Analysis';
      C.No_of_users__c = 5;
      C.CurrencyIsoCode = 'SEK';
      C.Monthly_Subscription__c = 100;
      C.Total_Subscription_Amount__c = 1000;
      C.One_Off_Amount__c = 0;
      C.First_Contract_Start_Date__c = Date.today();
      C.StartDate = Date.today();
      C.ContractTerm = 2;
      C.Running_Subscription__c = 'No';
    Insert C;
	
      C.Status = 'Activated';
      Update C;
      
      
		

        RestRequest request = new RestRequest();
        request.requestUri ='https://cs13.salesforce.com/services/apexrest/upsertaccount';
        request.httpMethod = 'POST';
		 String jsonBB = '{"make":" ' +C.Id +'","year":"2020"}' ; 
		request.requestBody =jsonBB ;
		
        RestContext.request = request;
		 ContractREST.doPost();
		
		
	}
}

 
This was selected as the best answer
Pedro SallesPedro Salles
Hi Raj Vakati 

Thanks for the help, I managed to make it work by adjusting in some specific points.

Best regards.