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
Navya sree 4Navya sree 4 

Test class failing for do not support getContent call

Hi,
Test Class Failing for Methods defined as TestMethod do not support getContent call


Test Class:
@isTest
private class PicklistDescriberTest {
 
    @isTest 
    static void test() {
        
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
         
      
        
        Account testAccount = new Account(Name='Test Account', Continent__c = 'Asia', Country__c = 'India', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
        
          
        
        System.assert(PicklistDescriber.describe(testAccount.Id, 'Asia').size()!=0);
        
        
        System.assert(PicklistDescriber.describe('Account', recordTypeNames[0], 'Continent__c').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Continent__c').size() > 0);
        
        System.assert(PicklistDescriber.describe('Account', recordTypeNames[0], 'Country__c', 'Continent__c').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypeNames[0], 'Country__c', 'Continent__c').size() > 0);
        
        System.assert(PicklistDescriber.describeWithDependency('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Country__c', 'Continent__c').size() > 0);
    }
    
}



Actual Class:

public with sharing class PicklistDescriber {
    static final Pattern OPTION_PATTERN = Pattern.compile('<option.+?>(.+?)</option>');

    /**
        Desribe a picklist field for an sobject id. RecordType is automatically picked
        based on the record's RecordTypeId field value.
        example usage :
        List<String> options = PicklistDescriber.describe(accountId, 'Industry');
    */
    public static List<String> describe(Id sobjectId, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'id' => sobjectId,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    /**
        Describe a picklist field for a SobjectType, its given record type developer name and the picklist field
        example usage :
        List<String> options = PicklistDescriber.describe('Account', 'Record_Type_1', 'Industry'));
    */
    public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeName' => recordTypeName,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    /**
        Describe a picklist field for a SobjectType, its given record type ID and the picklist field
        example usage :
        Id recType1Id = [Select Id from RecordType Where SobjectType = 'Account'
                                            AND DeveloperName like 'Record_Type_2'].Id;
        System.assertEquals(REC_TYPE_1_OPTIONS, PicklistDescriber.describe('Account', recType2Id, 'Industry'));
    */
    public static List<String> describe(String sobjectType, Id recordTypeId, String pickListFieldAPIName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeId' => recordTypeId,
                                                     'pickListFieldName'=> pickListFieldAPIName
                                                    }
                            );
    }

    public static List<String> describe(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
        return parseOptions(
                            new Map<String, String> {
                                                     'sobjectType' => sobjectType,
                                                     'recordTypeName' => recordTypeName,
                                                     'pickListFieldName'=> pickListFieldAPIName,
                                                     'contrfieldName' => contrfieldName
                                                    }
                            );
    }

    public static Map<String,List<String>> describeWithDependency(String sobjectType, String recordTypeName, String pickListFieldAPIName, String contrfieldName) {
        Map<String, List<String>> result = new Map<String,List<String>>();
        Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
        for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeName, pickListFieldAPIName, contrfieldName)) {
            for(String dep : depentOptions.keySet()) {
                result.put(dep, new List<String>());
                for(String val : depentOptions.get(dep)) {
                    if(val == pickval) {
                        result.get(dep).add(val);
                    }
                }
            }
        }

        return result;
    }

    public static Map<String,List<String>> describeWithDependency(String sobjectType, Id recordTypeId, String pickListFieldAPIName, String contrfieldName) {
        Map<String, List<String>> result = new Map<String,List<String>>();
        Map<String, List<String>> depentOptions = PicklistFieldController.getDependentOptionsImpl(sobjectType, contrfieldName, pickListFieldAPIName);
        for(String pickval : PicklistDescriber.describe(sobjectType, recordTypeId, pickListFieldAPIName)) {
            for(String dep : depentOptions.keySet()) {
                result.put(dep, new List<String>());
                for(String val : depentOptions.get(dep)) {
                        System.debug(val);
                        System.debug(pickval);
                    if(val == pickval) {
                        result.get(dep).add(val);
                    }
                }
            }
        }

        return result;
    }

    /*
        Internal method to parse the OPTIONS
    */
    static List<String> parseOptions(Map<String, String> params) {
        PageReference pr = Page.PicklistDesc;
        // to handle development mode, if ON
        pr.getParameters().put('core.apexpages.devmode.url', '1');

        for (String key : params.keySet()) {
            pr.getParameters().put(key, params.get(key));
        }

        String xmlContent = pr.getContent().toString();

        Matcher mchr = OPTION_PATTERN.matcher(xmlContent);
        List<String> options = new List<String>();
        while(mchr.find()) {
            System.debug(mchr.group(1));
            options.add(mchr.group(1));
        }
        // remove the --None-- element
        if (!options.isEmpty()) options.remove(0);
        return options;
    }
}
v varaprasadv varaprasad
Hi Navya,

Try this : 

Remove following line  :  String xmlContent = pr.getContent().toString();

Add below code : 
String xmlContent ;
if(Test.isRunningTest()) { 
  xmlContent = 'Unit.Test';
} else {
  xmlContent = pr.getContent().toString();
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
v varaprasadv varaprasad
Hi Navya,

Which line it is showing error or remove assert statements and check once.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
v varaprasadv varaprasad
Try This : 
 
@isTest
private class PicklistDescriberTest {
 
    @isTest 
    static void test() {
        
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
         
      
        
        Account testAccount = new Account(Name='Test Account', Continent__c = 'Asia', Country__c = 'India', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
        
          
         List<String> strs1 = PicklistDescriber.describe(testAccount.Id, 'Asia');
		 
		 List<String> strs2 = PicklistDescriber.describe('Account', recordTypeNames[0], 'Continent__c');
		 
		 List<String> strs3 = PicklistDescriber.describe('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Continent__c');
               
         List<String> strs4 = PicklistDescriber.describeWithDependency('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Country__c', 'Continent__c');
                
            }
    
}


 
v varaprasadv varaprasad
@isTest
private class PicklistDescriberTest {
 
    @isTest 
    static void test() {
        
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
         
      
        
        Account testAccount = new Account(Name='Test Account', Continent__c = 'Asia', Country__c = 'India', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
        
       
         List<String> strs1 = PicklistDescriber.describe(testAccount.Id, 'Continent__c');
		 
		
		 
		 List<String> strs2 = PicklistDescriber.describe('Account', recordTypeNames[0], 'Continent__c');
		 
		 List<String> strs3 = PicklistDescriber.describe('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Continent__c');
		 
		 List<String> strs4 = PicklistDescriber.describe('Account', recordTypeNames[0], 'Country__c','Continent__c');
               
         Map<String,List<String>> strs5 = PicklistDescriber.describeWithDependency('Account', recordTypes.get(recordTypeNames[0]).getRecordTypeId(), 'Country__c', 'Continent__c');
		 
		 Map<String,List<String>> strs6 = PicklistDescriber.describeWithDependency('Account', recordTypeNames[0], 'Country__c', 'Continent__c');
                
            }
    
}