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
Alias UchihaAlias Uchiha 

I want to write test class for this functions

Please help me to cover the percantage for the below line of code:-

public class testing1 {
  @AuraEnabled
    public static List<String> getObjectName(){
 List<String> objects=new List<String>();
       List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();  
        for(SObjectType sot:gd){
           objects.add(sot.getDescribe().getName());
           objects.sort();  
        }
        return objects;
    }
    
    @AuraEnabled
        public static List<String> getAllFields(String fld){
        List<String> fieldList = new List<String>();
Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sobjType = gd.get(fld);
Schema.DescribeSObjectResult describeResult = sobjType.getDescribe();
Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
    for(string str: fieldsMap.keySet()){
                fieldList.add(fieldsMap.get(str).getDescribe().getName());                
            }
    return fieldList;
}
    
    
       @AuraEnabled
        public static String getAllFieldsName(String fld1, String fld2){
       String ObjectName= fld2;
             system.debug('ObjectName '+ObjectName);
            String FieldName= fld1;
            system.debug('FieldName '+FieldName);
             system.debug('FieldName '+Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap().get(FieldName).getDescribe().getLabel());
        return Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap().get(FieldName).getDescribe().getLabel();
 
            //  Schema.DisplayType fielddataType;
      //      return  fielddataType.Schema.getGlobalDescribe().get(FieldName).getDescribe().getType();    
            
       // List<String> fieldList = new List<String>();
//Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//Schema.SObjectType sobjType = gd.get(fld1);
//Schema.DescribeSObjectResult describeResult = sobjType.getDescribe();
//Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
   // for(string str: fieldsMap.keySet()){
        //        fieldList.add(fieldsMap.get(str).getDescribe().getName());                
     //       }
 //   return fieldList;
            
            //Schema.SObjectField F = fieldNameMap.get(fieldAPIName); //where fieldAPIName is API name of field
//Schema.DescribeFieldResult R = F.getDescribe();
//String fieldLabel=R.getLabel();
//return fieldLabel;
}
    
      @AuraEnabled
        public static String getAllFieldsDataType(String fld11, String fld12){
     Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
        //String ac='Account';
     
        System.debug('sObject' +fld12);
        System.debug('fieldname' +fld11);
        Schema.SObjectType sobjType = gd.get(fld12);
        System.debug(sobjType);
        Schema.DescribeSObjectResult describeResult = sobjType.getDescribe();
        Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
        Schema.DisplayType fielddataType = fieldsMap.get(fld11).getDescribe().getType();
        String a;
        System.debug(fielddataType);
       // return Schema.getGlobalDescribe().get(objIdd).getDescribe().fields.getMap().get(fldId).getDescribe().getType();
         System.debug('return ' +fielddataType);
        return String.valueOf(fielddataType);
        }
}

Thanks
AnudeepAnudeep (Salesforce Developers) 
Hi Alias, 

I see that you have @AuraEnabled methods

Posting here an example of how to test AuraEnabled methods. Hope it helps
 
@AuraEnabled
public static String TestController(String username, String password) {
    try {
        String name = username;
        String pass = password;
        if(name == 'test@test.test' && pass == 'pass')
            return 'true';
    }
    catch (Exception ex) {
        return ex.getMessage();
    }
}

Test Class
 
@isTest
private class lightningForgotPasswordControllerTest
{
    @isTest
    static void TestForgotPassword()
    {
        String result = LightningForgotPasswordController.TestController('test@test.test', 'pass');
        System.assertEquals(null, result);
    }
}

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Akshay Dhiman 63Akshay Dhiman 63
Hi Allas,

Below test class works fine please this out:-

@isTest
private class testclassForTesting {
    @isTest static void  testCallout(){
        test.startTest();
        testing1.getObjectName();
        testing1.getAllFields('Account');
        System.assertEquals('Account Name',testing1.getAllFieldsName('Name','Account'));
        System.assertEquals('STRING',testing1.getAllFieldsDataType('Name','Account'));
        test.stopTest();
    }  
}

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay