-3

This question already has an answer here:

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);

marked as duplicate by battery.cord, glls, Pranay Jaiswal, codeyinthecloud, Reshma Feb 4 at 17:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

locked by Matt Lacey Feb 6 at 4:32

This post has been locked while disputes about its content are being resolved. For more info visit meta.

Read more about locked posts here.

  • 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 at 14:15
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.

  • 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 at 14:27
  • 1
    You need to set objName equal to 'Opportunity'. – David Reed Feb 4 at 14:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.