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
Vikas Kumar 135Vikas Kumar 135 

test data for @HTTP Post

Hi Everyone,
I have created a @HTTP get method which working fine.

@RestResource(urlMapping='/Opportunity/*')
global with sharing class OpportunityResource {
    @HttpGet
    global static Opportunity doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String opportunity_id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Opportunity result = [SELECT Id, Name, OwnerId, CurrencyIsoCode, IO_Status__c, Agency_Buyer__c, Campaign_Summary__c, Campaign_Goals__c, KPI_s_and_success_metrics__c, Ad_Ops_Lead__c, User__c, Campaign_Performance_Analyst__c, Account_Manager__c, Discount_Off_Rate_Card__c, Viewability_Tags__c, Additional_Tags__c, Opportunity_ID__c, Quote_Flight_Start_Date__c, Quote_Flight_End_Date__c, Quote_Total_Impressions__c FROM Opportunity WHERE Id = :opportunity_id];
        return result;
    }
}

I wrote a test class but I need help with inserting test data to the test class. Can anyone help me with writing the test class with test data?

Appreciate your help.
 
Best Answer chosen by Vikas Kumar 135
Raj VakatiRaj Vakati
Use this
 
@isTest
public class OpportunityResourceTest {
    public static testMethod void getPhoneUser() {

        Account a = new Account(Name = 'Test');
        insert a;
        Opportunity o = new Opportunity();
o.Amount=200 ;      
	  o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;

       Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/Opportunity/'+o.Id; 
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
      OpportunityResource .doGet();
       
   Test.stopTest();   
   
   
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use this
 
@isTest
public class OpportunityResourceTest {
    public static testMethod void getPhoneUser() {

        Account a = new Account(Name = 'Test');
        insert a;
        Opportunity o = new Opportunity();
o.Amount=200 ;      
	  o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;

       Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/Opportunity/'+o.Id; 
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
      OpportunityResource .doGet();
       
   Test.stopTest();   
   
   
    }
}

 
This was selected as the best answer
Vikas Kumar 135Vikas Kumar 135
Thanks, Raj. It worked