+ Start a Discussion
Holly Havelka 10Holly Havelka 10 

Help with Indicator Controller Test Class

Hi all,

I am struggling to write a test class for the below controller:
public class INDICATOR_Controller {
    private static SObject sobj;
    @AuraEnabled
    public static String getSObjectLabel(String sObjName){
        String label = Schema.getGlobalDescribe().get(sObjName).getDescribe().getLabel();
        return label;
    }
    @AuraEnabled
    public static List<INDICATOR_Service.Indicator> getIndicators(String recId, String objectName){
        getSObject(recId, objectName);
        if(sobj != NULL){
            List<INDICATOR_Service.Indicator> indicators = INDICATOR_Service.getIndicatorBadgesForObject(sobj);
            return indicators;
        } else{
            return null;
        }
    }
    public static void getSObject(String recId, String objectName){
        List<String> fNames = INDICATOR_Service.getFieldsToQueryForObject(objectName);
        if(fNames.size() > 0){
            String query = 'SELECT Id,'+ String.join(fNames,',')+' FROM '+ objectName +' WHERE Id =\''+ recId +'\' LIMIT 1';
            List<SObject> results = Database.query(query);
            if(results.size() == 1){
                sobj = results[0];
            }
        }
    }
}
Any thoughts on where to begin?  
 
Best Answer chosen by Holly Havelka 10
Steven NsubugaSteven Nsubuga
@isTest
private class INDICATOR_ControllerTest {

    @isTest static void testgetSObjectLabel(){
		System.assert(INDICATOR_Controller.getSObjectLabel('Account') != null);
    }
	
	@isTest static void testgetIndicators(){
		Account acct = new Account(Name = 'Test Account');
		insert acct;
		INDICATOR_Controller.getIndicators(acct.Id, 'Account');
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
@isTest
private class INDICATOR_ControllerTest {

    @isTest static void testgetSObjectLabel(){
		System.assert(INDICATOR_Controller.getSObjectLabel('Account') != null);
    }
	
	@isTest static void testgetIndicators(){
		Account acct = new Account(Name = 'Test Account');
		insert acct;
		INDICATOR_Controller.getIndicators(acct.Id, 'Account');
    }
}

 
This was selected as the best answer
Holly Havelka 10Holly Havelka 10
Thanks Steven!!
Steven NsubugaSteven Nsubuga
You're welcome Holly!