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
RahulRahul 

Hi friends, need help to write test class

/*
*Author: Vivek
*Date: 01/05/2018
*Description: Generic Domian Layer methods for SOQL/DML operations
*/
//TODO: add method headers
public virtual class AC_GenericDomainUtility{

//inserting sObjects
public list<sObject> insertSObjectLst(list<sObject> sObjLst){

        try{
            
            Database.insert(sObjLst);
            system.debug('*********in BD_ApplicationDomain, insert success, returning :'+sObjLst);
            return sObjLst;
        
        }catch(exception ex){
          //log exceptions in log object
          system.debug('*********Exception in BD_ApplicationDomain:'+ex.getMessage());
          //throw ex;
   
        }
        return Null;
        

}//insertSObjectLst


//updating sObjects
public list<sObject> updateSObjectLst(list<sObject> sObjLst){

        try{
            
            Database.update(sObjLst);
            system.debug('*********in BD_ApplicationDomain, update success, returning :'+sObjLst);
            return sObjLst;
        
        }catch(exception ex){
          //log exceptions in log object
          system.debug('*********Exception in BD_ApplicationDomain:'+ex.getMessage());
          //throw ex;
        }
        
        return Null;

}//updateSObjectLst

//fetching structure of all fields for respective object
public static string retrieveFieldLstForObject(String SobjectApiName){
        
        string query = '';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
 
        query = BD_InterfaceConstants.SELECTSTR +BD_InterfaceConstants.SPACE+ commaSepratedFields +BD_InterfaceConstants.SPACE+ BD_InterfaceConstants.FROMSTR+ BD_InterfaceConstants.SPACE + SobjectApiName ;
 
        return query;
        



//Fetching error codes for respective Service name and sending map<ErrorCode,Description>
public map<string,string> retrieveServiceErrorCodes(string servName){
   
   map<string,string> retMap = new map<string,string>();
   try{
     
     list<BD_GlobalErrorCodes__c> custLst = BD_GlobalErrorCodes__c.getAll().values();
     
     for(BD_GlobalErrorCodes__c cd: custLst){
       if(cd.BD_ServiceName__c.equals(servName)){
         retMap.put(cd.BD_ErrorCode__c,cd.BD_Description__c);
       }
     }
     
   }catch(Exception ex){
      //log ex
      system.debug('*********exception in BD_GenericDomainUtility.retrieveServiceErrorCodes:'+ex.getMessage());
   }
   
   return retMap;
}  

}

Thanks
Best Answer chosen by Rahul
Steven NsubugaSteven Nsubuga
@isTest
private class AC_GenericDomainUtilityTest {
    
    static testMethod void insertSObjectLstTestNoException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
        System.assert(new AC_GenericDomainUtility().insertSObjectLst(testAccTs) != null);
    }   
	
	static testMethod void insertSObjectLstTestException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT1);
        System.assertEquals(null, new AC_GenericDomainUtility().insertSObjectLst(testAccTs));
    }
	
	static testMethod void updateSObjectLstTestNoException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
		insert testAccTs;
		
		for (Account acct : testAccTs){
			acct.Name = acct.Name + datetime.now();
		}
        System.assert(new AC_GenericDomainUtility().updateSObjectLst(testAccTs) != null);
    }  

	static testMethod void updateSObjectLstTestException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
		insert testAccTs;
		
		for (Account acct : testAccTs){
			acct.Name = acct.Name + datetime.now();
		}
		Account testAccT3 = new Account(Name = 'util3');
		testAccTs.add(testAccT3);
        System.assertEquals(null, new AC_GenericDomainUtility().updateSObjectLst(testAccTs));
    }
	
	static testMethod void retrieveFieldLstForObjectTest() {
        System.assert(new AC_GenericDomainUtility().retrieveFieldLstForObject('Account') != null);
    }
	
	static testMethod void retrieveServiceErrorCodesTest() {
        System.assert(new AC_GenericDomainUtility().retrieveServiceErrorCodes('something') != null);
    } 
}

 

All Answers

Steven NsubugaSteven Nsubuga
@isTest
private class AC_GenericDomainUtilityTest {
    
    static testMethod void insertSObjectLstTestNoException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
        System.assert(new AC_GenericDomainUtility().insertSObjectLst(testAccTs) != null);
    }   
	
	static testMethod void insertSObjectLstTestException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT1);
        System.assertEquals(null, new AC_GenericDomainUtility().insertSObjectLst(testAccTs));
    }
	
	static testMethod void updateSObjectLstTestNoException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
		insert testAccTs;
		
		for (Account acct : testAccTs){
			acct.Name = acct.Name + datetime.now();
		}
        System.assert(new AC_GenericDomainUtility().updateSObjectLst(testAccTs) != null);
    }  

	static testMethod void updateSObjectLstTestException() {
        
        List<Account> testAccTs = new List<Account>();
        Account testAccT1 = new Account(Name = 'util1');
        Account testAccT2 = new Account(Name = 'util2');
        testAccTs.add(testAccT1);
        testAccTs.add(testAccT2);
		insert testAccTs;
		
		for (Account acct : testAccTs){
			acct.Name = acct.Name + datetime.now();
		}
		Account testAccT3 = new Account(Name = 'util3');
		testAccTs.add(testAccT3);
        System.assertEquals(null, new AC_GenericDomainUtility().updateSObjectLst(testAccTs));
    }
	
	static testMethod void retrieveFieldLstForObjectTest() {
        System.assert(new AC_GenericDomainUtility().retrieveFieldLstForObject('Account') != null);
    }
	
	static testMethod void retrieveServiceErrorCodesTest() {
        System.assert(new AC_GenericDomainUtility().retrieveServiceErrorCodes('something') != null);
    } 
}

 
This was selected as the best answer
RahulRahul
Hi steven, thanks for your reply. I got only 68% of code coverage
Steven NsubugaSteven Nsubuga
Hi Sumit, I would need to know more about your code in order to help you increase the code coverage. 
I do not know what is in the BD_GlobalErrorCodes__c for example.
I think you can figure out how to improve those tests to increase the coverage.
RahulRahul
Hi Steven, Global Error Codes is the custom settings. Please find the image 
User-added image
Steven NsubugaSteven Nsubuga
What about the data in the custom setting? Share it as well. 
RahulRahul
Hi Steven, Thank you so much. I managed to cover the other part :)