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
ram sbram sb 

Test Class Help for RestAPI

Could you please help me write test class for this?
@RestResource(urlMapping='/RESTService/*')
global with sharing class APPRestService {

@HttpGet
global static MergeWrap getempRecord() {
        MergeWrap Wrapper = new MergeWrap();
        Wrapper.EmployerList = [SELECT Name,Organization_Name__c,Organization_Logo__c,Organization_Street_Address__c,
                                                      Organization_Apt__c,Organization_City__c,Organization_County__c,Organization_Phone__c,
                                                      Organization_Phone_Ext__c,Organization_State__c FROM Organization__c];
        Wrapper.ApplicationList = [SELECT InstitutionName__c,Mailing_Address_Street_1__c,Mailing_Address_Street_2__c,
                                                        Mailing_Address_City__c,Mailing_Address_State__c,Mailing_Address_Zip__c, From Application__c];
           return Wrapper;
    }
    
    global class MergeWrap{
           global List<Application__c> ApplicationList;
           global List<Organization__c> EmployerList;
    }
 
}

 
Best Answer chosen by ram sb
Maharajan CMaharajan C
HI Ram,

Try the below code:
 
@isTest
private class SampleRestTest {

    @testSetup
    static void dataSetup() {
        Organization__c org = new Organization__c();
		org.Name = 'Salesforce';
		org.Organization_Street_Address__c ='Test Street';
		org.Organization_City__c ='Chennai';
		org.Organization_County__c='India';
		//// Add the remaining fields here to create the Organization__c record.
        insert org;
		
		Application__c app = new Application__c();
		app.InstitutionName__c = 'SFDC';
		app.Mailing_Address_City__c = 'Chennai';
		app.Mailing_Address_State__c = 'TN';
		//// Add the remaining fields here to create the Organization__c record.
		insert app;
    }

    static testMethod void testGet() {
		MergeWrap Wrapper = new MergeWrap();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/RESTService';
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;	
        Wrapper = APPRestService.getempRecord();
    }     
    
}

http://www.infallibletechie.com/2018/07/sample-inbound-rest-api-test-class-in.html
http://himanshurana.in/apex-rest-resource-test-class/

Thanks,
Maharajan.C​​​​​​​

All Answers

Maharajan CMaharajan C
HI Ram,

Try the below code:
 
@isTest
private class SampleRestTest {

    @testSetup
    static void dataSetup() {
        Organization__c org = new Organization__c();
		org.Name = 'Salesforce';
		org.Organization_Street_Address__c ='Test Street';
		org.Organization_City__c ='Chennai';
		org.Organization_County__c='India';
		//// Add the remaining fields here to create the Organization__c record.
        insert org;
		
		Application__c app = new Application__c();
		app.InstitutionName__c = 'SFDC';
		app.Mailing_Address_City__c = 'Chennai';
		app.Mailing_Address_State__c = 'TN';
		//// Add the remaining fields here to create the Organization__c record.
		insert app;
    }

    static testMethod void testGet() {
		MergeWrap Wrapper = new MergeWrap();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/RESTService';
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;	
        Wrapper = APPRestService.getempRecord();
    }     
    
}

http://www.infallibletechie.com/2018/07/sample-inbound-rest-api-test-class-in.html
http://himanshurana.in/apex-rest-resource-test-class/

Thanks,
Maharajan.C​​​​​​​
This was selected as the best answer
ram sbram sb
Thank you for the quick reponse!
following erros, Could you please suggest?
Line 23: Invalid type: MergeWrap
Line 30: illegal assignment from APPRestService.MergeWrap
 
Maharajan CMaharajan C
Sorry ram, i mentioned the wrapper wrongly in test class,

Change the line no: 23 as below in test class:

 APPRestService.MergeWrap Wrapper = new APPRestService.MergeWrap();

Thanks,
Maharajan.C
ram sbram sb
Thank you Maharajan!