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..

@RestResource(urlMapping='/IsPOrequiredForIP/*')
global class SalesforceCaseEntitlementCheck {
    

    @HttpGet
    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;
        caseList.add(c);
        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 (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm) there is some great information available there on how to approach your tests for these scenario's.
@@@we@@@@@@we@@@
static testMethod void getMeListOfProducts() {

 Test.startTest();
        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();
        System.debug(usr1);
   Test.stopTest();  
SFDC Apex DevSFDC Apex Dev

@@@we@@@

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

@@@we@@@@@@we@@@
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.
Akshay_DhimanAkshay_Dhiman
Hi Allen2,

Try this way out for creating your Test class for the Rest API,
Example -- 
@IsTest
private class CaseManagerTest {
    @isTest static void testGetCaseById() {
        Id recordId = createTestRecord();
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri =
            'https://yourInstance.salesforce.com/services/apexrest/Cases/'
            + 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',
            Status='New',
            Origin='Phone',
            Priority='Medium');
        insert caseTest;
        return caseTest.Id;
    }          
}



Thanks 
Akshay
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"

@isTest
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);
        
        
    }
}