+ Start a Discussion
SFDC Lightning 18SFDC Lightning 18 

Need help on Test Class for Apex class

Guys Please help me the below test calsses for apex.Thanks in advanced.

Apex class 1 : 
 
public with sharing class OpportunityEditForm_Ctrl {
    private static Map<String, Schema.FieldSet> fieldSetMap;
    
    @AuraEnabled
    public static FieldSetForm getForm(Id recordId, String objectName, String fieldSetName) {
        
        getObjectFieldSets(objectName);
        FieldSetForm form = new FieldSetForm();
        try{
            form.scopes = [SELECT Id, Name,Approved_Scope__c,Opportunity__c,Price_Type__c,Scope_Description__c,TotalValue__c 
                               FROM Scope__c WHERE Opportunity__c =: recordId];
            
            system.debug('===fieldSetName===='+fieldSetName);
            if(!String.isBlank(fieldSetName)){
                form.fields.addAll(getFields(fieldSetName));
        }
        
        }catch(Exception ex){
            System.debug('--- ' + ex.getMessage() + '--- ' + ex.getLineNumber());
            throw new AuraHandledException(ex.getMessage());
        }
        return form;
    }
    
    @AuraEnabled
    public static Id submitUIValues(String jsonObject, String recordId, List<Scope__c> lstScopes){
        Id returnId = null;
        try{
            System.debug('jsonObject ' + jsonObject);
            System.debug('recordId ' + recordId);
            Opportunity objOPP = (Opportunity) JSON.deserialize(jsonObject, Opportunity.Class);
            Map<Id, Opportunity> mapOpp = new Map<Id, Opportunity>{recordId => objOPP};
            system.debug('===mapOpp=='+mapOpp);
            getObjectFieldSets(Id.valueOf(recordId).getSobjectType().getDescribe().getName());
            List<String> lstFields = getFields('Clone_Opportunity_Field_Set');
            
            List<sObject> clonedOppty = SObjectAllFieldCloner.cloneObjects(new List<Id>{recordId}, Opportunity.getsObjectType(), 
                                                                          mapOpp, lstFields);
            if(clonedOppty.isEmpty()){
                return null;
            }
            objOPP = (Opportunity)clonedOppty[0];
            /* objOPP.Pricebook2Id =  [SELECT Id, Pricebook2Id FROM Opportunity WHERE Id =: recordId].Pricebook2Id;    */
            insert objOPP;
            
            Map<Id, Scope__c> oliMap = new Map<Id, Scope__c>(lstScopes);
            List<String> lstProductFields = new List<String>{
                'Name', 'Price_Type__c', 'TotalValue__c', 'Scope_Description__c'
            }; 
            List<Sobject> lstSobjectProducts = SObjectAllFieldCloner.cloneObjects(new List<Id>(new Map<Id, Scope__c>(lstScopes).KeySet()), 
                                                                                                             Scope__c.getsObjectType(),
                                                                                 oliMap, lstProductFields);
            
            for(Sobject OLI: lstSobjectProducts){
                OLI.put('Opportunity__c', objOPP.Id);
            }
            System.debug('------ lstSobjectProducts' + lstSobjectProducts);
            insert lstSobjectProducts;
            System.debug('------ lstSobjectProducts' + lstSobjectProducts);
            returnId = objOPP.Id;
        }
        catch(Exception ex){
            System.debug('--- ' + ex.getMessage() + '--- ' + ex.getLineNumber());
            throw new AuraHandledException(ex.getMessage());
        }
        return returnId;
    }
    
    private static void getObjectFieldSets(String objectName){
        Schema.SObjectType objectType = Schema.getGlobalDescribe().get(objectName);
        Schema.DescribeSObjectResult objectDescribe = objectType.getDescribe();
        fieldSetMap = objectDescribe.fieldSets.getMap();
    }
    
    
    private static List<String> getFields( String fieldSetName) {
        Schema.FieldSet fieldSet = fieldSetMap.get(fieldSetName);
        List<Schema.FieldSetMember> fieldSetMembers = fieldSet.getFields();
        List<String> fields = new List<String>();
        for (Schema.FieldSetMember fsm : fieldSetMembers) {
            fields.add(fsm.fieldPath);
        }
        return fields;
    }
    
    public class FieldSetForm {
        @AuraEnabled public List<String> fields;
        @AuraEnabled public List<Scope__c> scopes;
        public FieldSetForm() {
            fields = new List<String>();
            scopes = new List<Scope__c>();
        }
    }
}

Apex class 2 :
 
public class SObjectAllFieldCloner {
    
    // Clone a list of objects to a particular object type
    // Parameters
    // - List<sObject> sObjects - the list of objects to be cloned
    // - Schema.SobjectType objectType - the type of object to be cloned.
    // The sObjects you pass in must include the ID field,
    // and the object must exist already in the database,
    // otherwise the method will not work.
    public static List<sObject> cloneObjects(List<Id> sObjectIds, Schema.SObjectType objectType, 
                                                Map<Id, Sobject> mapSobject, List<String> lstFields){
        
        // A list of fields for the sObject being cloned
        List<String> sObjectFields = new List<String>();
        // A list of new cloned sObjects
        List<sObject> clonedSObjects = new List<sObject>();
        
        // Get all the fields from the selected object type using
        // the get describe method on the object type.
        if(objectType != null){
            sObjectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
        }
        /* Using the list of sObject IDs and the object type,
        we can construct a string based SOQL query
        to retrieve the field values of all the objects.*/
        
        String allSObjectFieldsQuery = 'SELECT ' + sObjectFields.get(0);
        
        for (Integer i=1 ; i < sObjectFields.size() ; i++){
            allSObjectFieldsQuery += ', ' + sObjectFields.get(i);
        }
        
        allSObjectFieldsQuery += ' FROM ' +
                                objectType.getDescribe().getName() +
                                ' WHERE ID IN: sObjectIds' ;
        
        
        System.debug('SOQL **** ' + allSObjectFieldsQuery);
        
        try{
            // Execute the query. For every result returned,
            // use the clone method on the generic sObject
            // and add to the collection of cloned objects
            for (SObject sObjectFromDatabase:Database.query(allSObjectFieldsQuery)){
                Sobject sobjFromCmp = mapSobject.get(sObjectFromDatabase.Id);
                for(String fieldAPI:lstFields){
                    system.debug('===='+fieldAPI);
                    sObjectFromDatabase.put(fieldAPI, sobjFromCmp.get(fieldAPI));
                }
                if(objectType.getDescribe().getName() == 'OpportunityLineItem'){//added this because it was throwing error like we can't provide unitprice and total together
                    sObjectFromDatabase.put('TotalPrice', null);
                }
                clonedSObjects.add(sObjectFromDatabase.clone(false,true));
            }
            System.debug('clonedSObjects **** ' + clonedSObjects);
        } catch (exception e){
            System.debug('======>>'+e);
        }
        return clonedSObjects;
    }
}