0

Having some issues creating a test class for controller. It's assisting a lightning component that loads record types for the cases object instead of hardcoding it. I bet I'm over thinking it. Would love some helpful insight on how to test for this.

public class recordtypeController {
public static Map<Id, String> recordtypemap {get;set;}

@AuraEnabled        
public static List<String> fetchRecordTypeValues(){
    List<Schema.RecordTypeInfo> recordtypes = 
Case.SObjectType.getDescribe().getRecordTypeInfos();    
    recordtypemap = new Map<Id, String>();
    for(RecordTypeInfo rt : recordtypes){
        if(rt.getName() != 'Master')
        recordtypemap.put(rt.getRecordTypeId(), rt.getName());
    }        
    return recordtypemap.values();
}

@AuraEnabled
public static Id getRecTypeId(String recordTypeLabel){
    Id recid = Schema.SObjectType.Case.getRecordTypeInfosByName().get(recordTypeLabel).getRecordTypeId();        
    return recid;
 }      
}

Test Class:

 @isTest
 public class recordtypeControllerTest {


private static testmethod void testFetchRecordTypes() {
    List<String> values = recordtypeController.fetchRecordTypeValues();
}

private static testmethod void testgetRecordTypeId() {
    string recordTypeLabel = 'Customer Service';
    ID testId = recordtypeController.getRecTypeId(recordTypeLabel);
   } 
 }
  • 1
    can you post your code whatever you have tried so far – Santanu Boral Jul 1 '17 at 3:08
  • seems you do not need to create any records to test your code. It is just describe information. call methods and assert vales. done – Eric Jul 1 '17 at 4:52
  • Thank you Eric. I never had to write a test class for this type of controller before. However one issue is that I'm passing in 'recordTypeLabel' from the lightning component client controller. I don't see any documentation on how to test the method without this variable. Getting errors. – Michael Yocca Jul 1 '17 at 5:05
  • It's just a string. Do a describe, get a record type name, pass that in and assert the right id comes back – Eric Jul 1 '17 at 7:27
  • Updated my existing code. Have 90% coverage but have a Nullpointer exception which I think I know why. And have a Stack Trace which I believe to be due to a logic error in my test class. – Michael Yocca Jul 10 '17 at 18:28
0
@isTest
public class recordtypeControllerTest {


static testmethod void testFetchRecordTypes() {
    List<String> values = recordtypeController.fetchRecordTypeValues();
  }

 static testmethod void testgetRecordTypeId() {
    String recordTypeLabel = 'Customer Service';
    ID testId = recordtypeController.getRecTypeId(recordTypeLabel);
    System.assert(testId != null);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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