+ Start a Discussion
Sujendran Sundarraj 8Sujendran Sundarraj 8 

test class for Schema.SobjectType

Dear all, 
Kindly help me to write the test class for the below code. 
public class Workbenchlikes {
    public string strObjectName {get;set;}
    public list<SelectOption> lstObjNames {get; set;}
    public list<selectOption> lstfieldNms {get; set;}
    public list<string> getfields {get; set;} 
    public string selval {get; set;}
    public list<selectOption> selvalues {get; set;}
    public boolean test {get; set;}
    public string inputrec {get; set;}
    public workbenchlikes(){
        retriveObjectNames();
    }
    
    public void retriveObjectNames() {
        lstObjNames = new list<SelectOption>();
        lstObjNames.add(new selectOption('==Select One==', '==Select One==', false));
        map<string, schema.sobjectType> selectobjs = schema.getGlobalDescribe();
        for ( schema.sobjecttype objIterator : selectobjs.values()) {
            Schema.DescribeSObjectResult objResult = objIterator.getDescribe();
            lstObjNames.add(new SelectOption(objResult.getname(), objResult.getlabel()));
        }
               system.debug('lstObjNames ===>  '+lstObjNames);
    }
    
    public void retriveFieldNames(){
    lstfieldNms = new list<selectOption>();
    
        system.debug('strObjectName ======>  ' +strObjectName);
        
        
        schema.sobjecttype obj = schema.getglobaldescribe().get(strObjectName);
        if(obj !=null){
        Map<string, schema.sobjectField> objfield = obj.getdescribe().fields.getMap();
        for(schema.sobjectfield fiIterator : objfield.values()){
        schema.describefieldresult fieldres =fiIterator.getDescribe();
        lstfieldNms.add(new selectoption(fieldres.getname(), fieldres.getname()));
        }    
        }else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select An Object'));
        }
       
    }
    public void clearsel(){
    if(selvalues != null){
    
     selvalues.clear();
     test = false;
     }
    }
    public void getquery(){
    selvalues = new list<selectOption>();
    if(getfields.size() == 0){
    selvalues.clear();
    }else{
    test = true;
    system.debug('test'+test);
    for(integer i=0; i< getfields.size(); i++){
selvalues .add(new selectOption(getfields[i], getfields[i]));
    }
    }
    }
    public void query(){
    map<integer,string> strl = new map<integer,string>();
    for(integer i=0 ; i<getfields.size(); i++){
    strl.put(i,getfields[i]);
    }
    system.debug('strl===>' +strl);
    inputrec = 'select '+string.join(strl.values(),',')+' from ' +strObjectName;
    }
    }

For me the code coverage is 52%. Kindly help!!

Thank you. 

Regards, 
Sujendran. 

Raj VakatiRaj Vakati
Use this 
@isTest
private class Workbenchlikestest {

    private static testmethod void m1(){
         Account acc = new Account(name='DEmo');
        insert acc;
        
        Workbenchlikes w = new Workbenchlikes() ;
        w.strObjectName='Account';
        List<String> stFl = new List<String>() ;
        stFL.add('Name') ;
        w.getfields =stFl ;
       
        w.retriveObjectNames();
        w.retriveFieldNames();
        w.clearsel();
        w.query();
        w.getquery();
        
        
    }
}