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
SFDC Apex DevSFDC Apex Dev 

Could anyone help me out to write the test class for this apex class. It has rest resource and m not familiar with it..

global class SalesforceCaseEntitlementCheck {

    global static AssetCls isPOrequired() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String IP_ID = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        system.debug('APR  --> '+IP_ID );
        case c = new case();
        List<Case> caseList = new List<Case>();
        c.RecordTypeId = SMCUtils.getRTMap().get(P_Constant.NameTechnical_Support);
        c.SV__Component__c = IP_ID;
        SMCUtils.updateCaseFromComponent(caseList, null, false);
        SMCUtils.updateCaseEntitlementFromContract(caseList, null);
        system.debug('Is Po Required --> '+c.SV_Require_PO__c );
        return (new AssetCls(IP_ID, c.SV_Require_PO__c));
    global class AssetCls {
        public String assetId;
        public Boolean poRequired;

        public AssetCls (String assetId, Boolean poRequired) {
            this.assetId = assetId;
            this.poRequired = poRequired;
Pim de VosPim de Vos
Is there anything particular that you seem to be hanging on?
I would start of by reading up a bit on: Rest Mocking ( there is some great information available there on how to approach your tests for these scenario's.
static testMethod void getMeListOfProducts() {

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/claimApi';
        req.params.put('phoneNumber', '9876543210');
        req.params.put('claimNumber', '111111');
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json');
        RestContext.request = req;
        RestContext.response = res;
        User usr1 = UserClaimApi.getClaimUser();
SFDC Apex DevSFDC Apex Dev


ur code is not for my class man, not a single word.

Can you  proived screen shot
SFDC Apex DevSFDC Apex Dev
what all are these claim user claim api??  even my class is about the case and asset.
Hi Allen2,

Try this way out for creating your Test class for the Rest API,
Example -- 
private class CaseManagerTest {
    @isTest static void testGetCaseById() {
        Id recordId = createTestRecord();
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri =
            + recordId;
        request.httpMethod = 'GET';
        RestContext.request = request;
        // Call the method to test
        Case thisCase = CaseManager.getCaseById();
        // Verify results
        System.assert(thisCase != null);
        System.assertEquals('Test record', thisCase.Subject);
 static Id createTestRecord() {
        // Create test record
        Case caseTest = new Case(
            Subject='Test record',
        insert caseTest;
        return caseTest.Id;

SFDC Apex DevSFDC Apex Dev
I have written this much... calling the method
but not covering anything and error occurs "System.NullPointerException: Attempt to de-reference a null object"

public class SalesforceCaseEntitlementCheck_Test {
    static testmethod void testIsPOrequired(){
        case c = new case();
        c.RecordTypeId = ServiceMaxCaseUtils.getRTMap().get(PBIMPACT_Constant.NameTechnical_Support);
        insert c;
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/IsPOrequiredForIP/';
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;
        SalesforceCaseEntitlementCheck.AssetCls SA = SalesforceCaseEntitlementCheck.isPOrequired();
        System.assertEquals('12vvb' ,SA.assetId);
        System.assertEquals(False ,SA.poRequired);