Skip to main content
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;

    }

}
4 respostas
  1. 10 de jan. de 2019, 06:51
    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();

    }

    }

     
0/9000