-3

I am trying to cover the below class. I am getting 71%. Can you please let me know how to cover .

public class OpportunityRecordTypeSelectionCtr {

@AuraEnabled
public static string findRecordTypes(string objName){
    string returnString='';
    string queryString='Select id, name from RecordType where sobjectType =: objName and IsActive=true ORDER BY Name DESC';
    List<sobject> recordList= Database.query(queryString);
    List<RecordTypeWrapper> wrapperList=new List<RecordTypeWrapper>();
    for(sobject sb : recordList)  {
        RecordTypeWrapper rw=new RecordTypeWrapper();
        rw.recordTypeLabel=string.valueof(sb.get('name'));
        rw.recordTypeId=string.valueof(sb.get('id'));
        wrapperList.add(rw);
    } 
    returnString= JSON.serialize(wrapperList);
    system.debug('*****'+returnString);
    return returnString;
}
public class RecordTypeWrapper{
    public string recordTypeLabel{get;set;}
    public string recordTypeId{get;set;}
}

}

Test class:

@isTest
private class OpportunityRecordTypeSelectionCtr_Test {
    static testMethod void testRecordTypes(){
    string objName;
    OpportunityRecordTypeSelectionCtr.findRecordTypes(objName);
      string recordTypeLabel;
        string recordTypeId;
    OpportunityRecordTypeSelectionCtr.RecordTypeWrapper recordTypeWrap = new OpportunityRecordTypeSelectionCtr.RecordTypeWrapper();
     recordTypeWrap.recordTypeLabel = recordTypeLabel;
        recordTypeWrap.recordTypeId = recordTypeId;
    }
}

Unable to cover below lines of code.

RecordTypeWrapper rw=new RecordTypeWrapper();
            rw.recordTypeLabel=string.valueof(sb.get('name'));
            rw.recordTypeId=string.valueof(sb.get('id'));
            wrapperList.add(rw);
1
  • 2
    Please do not destroy the contents of your question. Doing so invalidates answers and removes value for the community. I have rolled back your edit.
    – David Reed
    Feb 5, 2019 at 14:15

1 Answer 1

3

Your test class is invoking your code with a null sObject name value.

string objName;
OpportunityRecordTypeSelectionCtr.findRecordTypes(objName);

objName is uninitialized and null. Your query therefore returns an empty list.

string queryString='Select id, name from RecordType where sobjectType =: objName and IsActive=true ORDER BY Name DESC';

and recordList is therefore empty, and the for loop never executes.

for(sobject sb : recordList)  {

Test with the name of an object that has record types in your org.

2
  • I am using sobject as Opportunity and it has two record types 1. Standard Opportunity 2. Opportunity Grouping. Can u please let me know how to refer these objects in test class
    – SFDC Guest
    Feb 4, 2019 at 14:27
  • 1
    You need to set objName equal to 'Opportunity'.
    – David Reed
    Feb 4, 2019 at 14:28

Not the answer you're looking for? Browse other questions tagged .