+ Start a Discussion
Guru 91Guru 91 

How to get test coverage for this method?

Hi All,
I am wring test class and i got only 25% code coverage and unable to achive test coverage for two methods


 private static String  appendWhere(String query,String whereCon ){
          if(whereCon!=''){
             String Id = whereCon.Split('\'')[1];
             String str= whereCon;
             integer tot= str.countMatches(' AND ');
             system.debug('----------Test-------'+tot); 
             String acctIds =OV_BAUtility.getAcctIds(Id);
             if(tot==2){
             String andClause = (whereCon.contains('AND')?' AND '+whereCon.Split('AND')[1]+' AND '+whereCon.Split('AND')[2]:'');
             query=query +' where '+whereCon.Split('=')[0]+' IN '+ acctIds +andClause;   
             }else{
             String andClause = (whereCon.contains('AND')?' AND '+whereCon.Split('AND')[1]:'');
             query=query +' where '+whereCon.Split('=')[0]+' IN '+ acctIds +andClause;
             }
           
         }
         return query;
     }
    public static void setTotal(List<sobject> sObjectsList,String totalFields,lightningTableWrapper wrp){
        List<String> total =totalFields.split(',');
        system.debug('-----------------------------------------------tttttt-------------='+totalFields+'=----------'+sObjectsList+'total.size()---'+total.size());
        Map<String,Decimal > totalmap= new Map<String,Decimal >();
        if(total.size() > 1){
            for(sObject obj :sObjectsList){
                for(String t:total){
                    t=t.trim();
                    if(totalmap.containsKey(t)){
                        totalmap.put(t,totalmap.get(t)+(Decimal )obj.get(t));
                    }else{
                        totalmap.put(t,(Decimal )obj.get(t));
                    }
                }
            }
        }
        wrp.totalFields=totalmap;
    }

Thanks
Niraj Kr SinghNiraj Kr Singh
Hi Guru,

If you want to call private method to cover in test class, you should use "@TestVisible" just before the private method in your Apex.
Like this:
@TestVisible
private static String  appendWhere(String query,String whereCon ){ ......... }
@isTest private class TestVisibleExampleTest { 
	@isTest static void test1() { 
		List<sobject> sObjectsList = new List<sobject>();
		for(Integer index = 1; index <= 5 index++) {
			sObjectsList.add(new Account(Name='Test-' + index)); //Take your relavent object here if needed
		}
		insert (list<Account>sObjectsList);
		sObjectsList.clear();
		sObjectsList = [Select Id, Name, Rating, Phone From Account];
		system.assertEquals(sObjectsList.size(),5)
		String totalFields = 'Name,Rating,Phone';
		lightningTableWrapper objWrapper = new lightningTableWrapper(); //update according to your wrapper class here
		
		String query = 'Select Name From Account';
		String whereCon = ' Where Name Like test\'%\''; //form your where condition here
		Test.startTest();
			YourClassName.setTotal(sObjectsList, totalFields, objWrapper);
			YourClassName.appendWhere(query, whereCon);
		Test.stopTest();
	} 
}
If it helps you, mark your ans !!
Thanks
Niraj