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 

Help in code coverage?

Hi Everyone,
Please help me with test class

​public class PicklistDescController {
    public Sobject sobj {get;set;}
    public String pickListFieldName {get;set;}
    public String contrfieldName {get; set;}

    public PicklistDescController() {
        Map<String, String> reqParams = ApexPages.currentPage().getParameters();
        String sobjId = reqParams.get('id');
        String recordTypeId = reqParams.get('recordTypeId');
        String recordTypeName = reqParams.get('recordTypeName');
        String sobjectTypeName = reqParams.get('sobjectType');
        this.pickListFieldName = reqParams.get('picklistFieldName');
        this.contrfieldName = (reqParams.get('contrfieldName') != null) ? reqParams.get('contrfieldName') : '';

        Schema.SobjectType sobjectType = null;

        if (sobjectTypeName != null && sobjectTypeName.trim().length() > 0) {
            sobjectType = Schema.getGlobalDescribe().get(sobjectTypeName);
            // create blank sobject record
            sobj = sobjectType.newSobject();

            // if no recordTypeId passed explicitly by user, try loading one from the RecordType table
            if (isBlank(recordTypeId) && !isBlank(recordTypeName)) {
                // queryexception is fine, we don't want to return anything good for bad recordtype
                RecordType recType = [Select Id from RecordType Where SobjectType =:sobjectTypeName
                                            AND DeveloperName like :recordTypeName];
                recordTypeid = recType.id;
            }
            sobj.put('RecordTypeId', recordTypeid);

        } else if (sobjId != null && sobjId.trim().length() > 0) {
            // find the so
            for (SobjectType sobjType : Schema.getGlobalDescribe().values()){
                String sobjPrefix = sobjType.getDescribe().getKeyPrefix();
                if (sobjPrefix == null) continue;
                System.debug('SobjectType ' + sobjType + ', ' + sobjPrefix);
                if (sobjId.toLowerCase().startsWith(sobjPrefix.toLowerCase())) {
                    sobjectType = sobjType;
                    break;
                }
            }
            // following not working with input:field
            //sobj = sobjectType.newSobject(sobjId);
            sobj = Database.query ('SELECT ' + pickListFieldName + ((contrfieldName != '') ? ', ' + contrfieldName : contrfieldName) + ' FROM ' + sobjectType + ' WHERE ID =:sobjId');
        }

    }

    static boolean isBlank(String val) {
        return val == null || val.trim().length() == 0;
    }
}
Steven NsubugaSteven Nsubuga
Here is a test for you. I used the Account object, with 2 record types, as well as a Controlling and Dependent picklist fields.
@isTest
public class PicklistDescControllerTest {

    @isTest static void testAllParams(){
		
        // There must be at least 1 record type  for Account
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
        
        // Reason__c is a dependent picklist, Rating is controlling picklist
        // If Stage is Hot, the valid values for Reason are Many deals or Great staff
        // If Stage is Cold, the valid values for Reason are Unresponsive staff or No deals
        // If Stage is Warm, the valid value for Reason is Responsive staff
		Account testAccount = new Account(Name='Test Account',Rating = 'Hot', Reason__c = 'Many deals', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
		
         
        
		PageReference pageRef = Page.sample; // Your Page Name
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
        pageRef.getParameters().put('recordTypeId', String.valueOf(recordTypes.get(recordTypeNames[0]).getRecordTypeId()));
        pageRef.getParameters().put('recordTypeName', String.valueOf(recordTypeNames[0]));
        pageRef.getParameters().put('sobjectType', String.valueOf(testAccount.getSObjectType()));
        pageRef.getParameters().put('picklistFieldName', 'Reason__c');
        pageRef.getParameters().put('contrfieldName', 'Rating');
        
        Test.setCurrentPage(pageRef);
		
		PicklistDescController controller = new  PicklistDescController(); 
		System.assert(controller.sobj != null);
        System.assert(controller.pickListFieldName != null);	
        System.assert(controller.contrfieldName != null);	
    }
    
    @isTest static void testBlankRecordTypeId(){
		
        // There must be at least 1 record type  for Account
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
        
        // Reason__c is a dependent picklist, Rating is controlling picklist
        // If Stage is Hot, the valid values for Reason are Many deals or Great staff
        // If Stage is Cold, the valid values for Reason are Unresponsive staff or No deals
        // If Stage is Warm, the valid value for Reason is Responsive staff
		Account testAccount = new Account(Name='Test Account',Rating = 'Hot', Reason__c = 'Many deals', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
		
         
        
		PageReference pageRef = Page.sample; // Your Page Name
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
        pageRef.getParameters().put('recordTypeId', '');
        pageRef.getParameters().put('recordTypeName', String.valueOf(recordTypeNames[0]));
        pageRef.getParameters().put('sobjectType', String.valueOf(testAccount.getSObjectType()));
        pageRef.getParameters().put('picklistFieldName', 'Reason__c');
        pageRef.getParameters().put('contrfieldName', 'Rating');
        
        Test.setCurrentPage(pageRef);
		
		PicklistDescController controller = new  PicklistDescController(); 
		System.assert(controller.sobj != null);
        System.assert(controller.pickListFieldName != null);	
        System.assert(controller.contrfieldName != null);	
    }
    
    @isTest static void testNoSobjectType(){
		
        // There must be at least 1 record type  for Account
        Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();
        List<String> recordTypeNames = new List<String>(recordTypes.keyset());
        
        // Reason__c is a dependent picklist, Rating is controlling picklist
        // If Stage is Hot, the valid values for Reason are Many deals or Great staff
        // If Stage is Cold, the valid values for Reason are Unresponsive staff or No deals
        // If Stage is Warm, the valid value for Reason is Responsive staff
		Account testAccount = new Account(Name='Test Account',Rating = 'Hot', Reason__c = 'Many deals', RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId());
        insert testAccount;
		
         
        
		PageReference pageRef = Page.sample; // Your Page Name
		pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
        pageRef.getParameters().put('recordTypeId', '');
        pageRef.getParameters().put('recordTypeName', String.valueOf(recordTypeNames[0]));
        pageRef.getParameters().put('picklistFieldName', 'Reason__c');
        pageRef.getParameters().put('contrfieldName', 'Rating');
        
        Test.setCurrentPage(pageRef);
		
		PicklistDescController controller = new  PicklistDescController(); 
		System.assert(controller.sobj != null);
        System.assert(controller.pickListFieldName != null);	
        System.assert(controller.contrfieldName != null);	
    }
    
}