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
keshin okawakeshin okawa 

N>Help Creating Test Class for Retrieving Field Names

I'm used to making test classes for inserting data in Objects but I'm kinda lost in making test data for this code...
(It retrieves the custom field names of my Custom Object mMovie__c)

public class objectlist {

  public List<string> fieldLabel {get;set;}
  
  public objectlist()
  {
    this.getAccountFields();
  }

  public void getAccountFields()
  {
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
          fieldLabel = new List<string>();      
          Map <String, Schema.SObjectField> fieldMap = gd.get('mMovie__c').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {     
              // if(fieldAPI.getDescribe().isCustom() )
              //{
               fieldLabel.add(fieldAPI.getDescribe().getLabel());
              //}
          }
          fieldLabel.sort();
          system.debug('fieldLabel-----'+fieldLabel);
  }     
}

 
Best Answer chosen by keshin okawa
surasura

Hi 

since you are  not using any data in your class you dont have to create any data to test the class , just use below code . it will give 100% coverage .
 
@isTest
private class ApexTest_ObjectList
{
     static testMethod void Testmethod1()
    {

        objectlist ol = new objectlist();
   }



}


 

All Answers

surasura

Hi 

since you are  not using any data in your class you dont have to create any data to test the class , just use below code . it will give 100% coverage .
 
@isTest
private class ApexTest_ObjectList
{
     static testMethod void Testmethod1()
    {

        objectlist ol = new objectlist();
   }



}


 
This was selected as the best answer
keshin okawakeshin okawa
Thx again sura...I didn't know that you could test a class by creating an instance of it (objectlist). Another new thing I learned today...
surasura
to  test the non static classes specialy visualforce page controllers and extensions you have to create a instance of that class , most of the time scenarios are much complicated than yours . since your class constructor call the method getAccountFields() you only needed to instantiate the class