+ Start a Discussion
Ajith Selvaraj 2Ajith Selvaraj 2 

Methods defined as TestMethod do not support getContent call in test class

public static workOrderRecordFormApex SAPgetPicklistValues(string recordTypeId, string objectName,string fieldApiName){
        Map<String, String>  piclistValues=new Map<String, String>();
        workOrderRecordFormApex objs = new workOrderRecordFormApex();
        Httprequest req = new HttpRequest();
        String SESSION_ID_START = 'SESSION_ID_START';
        String SESSION_ID_END = 'SESSION_ID_END';
        String pageContent = Page.SessionId.getContent().toString();
        Integer startIndex = pageContent.indexOf(SESSION_ID_START) + SESSION_ID_START.length();
        Integer endIndex = pageContent.indexOf(SESSION_ID_END);
        String sessionId =pageContent.substring(startIndex, endIndex);
          req.setEndpoint(
            URL.getSalesforceBaseUrl().toExternalForm() +
            '/services/data/v50.0/ui-api/object-info/' +
            objectName +
            '/picklist-values/' +
            recordTypeId +
            '/' +
            fieldApiName
        );
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
           req.setHeader('Authorization', 'Bearer ' + sessionId);
        Http http = new Http();
        httpresponse response=http.send(req);
        picklistWrapper obj=new picklistWrapper();
        obj=(picklistWrapper)JSON.deserialize(response.getBody(), picklistWrapper.class);
        String JSONstr = response.getBody();
        JSONParser parser = JSON.createParser(JSONstr);
        Map<String, Object> defaultValueMap = (Map<String, Object>)JSON.deserializeUntyped(JSONstr);
        for(piclistValues pickVal:obj.values){
            
            piclistValues.put(pickVal.value, pickVal.label);
        }
        objs.defaultSAPOrderType = defaultValueMap.get('defaultValue');
        objs.sapOrderTypeValues = piclistValues;
        system.debug('objs' + objs);
        return objs;
    }


    private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> PLEs) {
        return (List<PicklistEntryWrapper>)
            JSON.deserialize(JSON.serialize(PLEs), List<PicklistEntryWrapper>.class);
    }
    public class picklistWrapper{
        @AuraEnabled public piclistValues[] values;
    }
    public class piclistValues {
        @AuraEnabled public String label;    
        @AuraEnabled public String value;
    }
    public class PicklistEntryWrapper{
        public String active {get;set;}
        public String defaultValue {get;set;}
        public String label {get;set;}
        public String value {get;set;}
        public String validFor {get;set;}
        public PicklistEntryWrapper(){            
        }
        
    }
    @AuraEnabled
    public object defaultSAPOrderType {get; set;}
    @AuraEnabled
    public Map<String, String> sapOrderTypeValues {get; set;}
    public class SObJectResult {
        @AuraEnabled
        public String recName;
        @AuraEnabled
        public Id recId;
        
        public SObJectResult(String recNameTemp, Id recIdTemp) {
            recName = recNameTemp;
            recId = recIdTemp;
        }
    }


In this above code Im getting the picklist value for field, where the values is based on record type, the code is working fine, But for the test class Im calling that method like 
QuoteComponentCtrl.SAPgetPicklistValues(salesRecordTypeId, 'WorkOrder', 'SAP_Order_Type__c');
But Im getting error as Methods defined as TestMethod do not support getContent call. I have created on vf page name as SessionId to get the end point. Kindly help me to resolve the error in test class
ShivankurShivankur (Salesforce Developers) 
Hi Ajith,

You may consider writing your test class using mock callouts .Never use get content inside test class.

Please refer to below developer guide to implement a Test class for methods with Webservice callout in Apex:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm

More reference:
https://jayakrishnasfdc.wordpress.com/2021/01/02/apex-test-class-examples-for-web-services/

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.