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
ManojKumar MuthuManojKumar Muthu 

Test class for HttpGet method

Hi there,
I am struggling to write a test class for below, can some help on this asap?

@RestResource(urlMapping='/CaseAccountid/*')
global with sharing class CaseAccountid{

@HttpGet
    global static List<Case> getCaseById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String Id = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<case> result =  [Select id, IsEscalated, Case_Owner_Name__c, OwneEmail__c, subject, Case_Type__c, Code__c, Tracker__c, status, Version__c, type, priority, ContactEmail, ContactId, OwnerId, CaseNumber, CSM_ID__c, CUP_Phone__c, Call_Type__c from Case where AccountId = :Id];
        return result;
    }
}
Best Answer chosen by ManojKumar Muthu
Raj VakatiRaj Vakati
Use this code
 
@isTest
private class CaseAccountidTest {
    @isTest static void testCaseOne() {
    
	Test.startTest();
	
	

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
  
    
    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
    insert c;
	
	

   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/CaseAccountid/'+c.accountId;  //Request URL
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    CaseAccountid.getCaseById();
    Test.stopTest();

   
	
	
	
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Here is the code
 
@isTest
private class CaseAccountidTest {
    @isTest static void testCaseOne() {
    
	Test.startTest();
	
	

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
  
    
    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
    insert c;
	
	

   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/CaseAccountid/'c.accountId+;  //Request URL
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    CaseAccountid.getCaseById();
    Test.stopTest();

   
	
	
	
    }
    
}

 
ManojKumar MuthuManojKumar Muthu
Hi Raj,

Thanks for taking effort to reply for my issue, 
when i run the above your I am getting these error
Missing ';' at 'c.accountId'
Unexpected token ';'.
Variable does not exist: JsonMsg

Can me help me out?
 
Raj VakatiRaj Vakati
Use this code
 
@isTest
private class CaseAccountidTest {
    @isTest static void testCaseOne() {
    
	Test.startTest();
	
	

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
  
    
    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
    insert c;
	
	

   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

    req.requestURI = '/services/apexrest/CaseAccountid/'+c.accountId;  //Request URL
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    CaseAccountid.getCaseById();
    Test.stopTest();

   
	
	
	
    }
    
}

 
This was selected as the best answer
ManojKumar MuthuManojKumar Muthu
Now its work fine, u have no idea how much u have helped me...

Thank u sooo much