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
Mohammed ArshadMohammed Arshad 

test class to cover a list methods

Hi Guys,

I am unable to write a test class for the below code, Can someone help me:
Controller:
--------------
public with sharing class IS_Retailer {       
    public IS_Retailer() {
    }
    public List<IS_Checker> check(IS_Retail retail) {
        List<IS_Checker> checkErrors= new List<IS_Checker>();
        if(retail.appAmount < 5000){
            IS_Checker ch = new IS_Checker('check', The price is less than Rs:5000');
            checkErrors.add(ch);
        }
        checkErrors.addAll(IsThere(retail.soapList,'Atleast 1 ABC is required'));
        checkErrors.addAll(IsThere(retail.foamList,'Atleast 1 XYZ is required'));
        return checkErrors;
    }
    public List<IS_Checker> IsThere(List<SObject> objList, String err) {
        List<IS_Checker> checkErrors= new List<IS_Checker>(); 
        if(objList != null){
            if(objList.isEmpty()){
               IS_Checker ch = new IS_Checker('Check', err);
               checkErrors.add(ch);          
            }
        }
        return checkErrors;
    }   
}
Niraj Kr SinghNiraj Kr Singh
Hi Mohammed,
Try this, might be needed to update other required fields whhile creating records for test then add it.
@isTest private class TestClass { 
	@isTest static void test1() { 
		
		//Considering IS_Retail is an custom object.
		IS_Retail objRetail1 = new IS_Retail();
		objRetail1.appAmount = 1000;
		//Add here other required fields.
		
		Test.startTest();
			IS_Retailer  objClass = new IS_Retailer();
			List<IS_Checker> lstReturn = objClass.check(objRetail1);
			system.assert(lstReturn != null);
		Test.stopTest();
	} 
	
	@isTest static void test2() { 
		
		//Considering IS_Retail is an custom object.
		IS_Retail objRetail1 = new IS_Retail();
		objRetail1.appAmount = 6000;
		//Add here other required fields.
		
		Test.startTest();
			IS_Retailer  objClass = new IS_Retailer();
			List<IS_Checker> lstReturn = objClass.check(objRetail1);
			system.assert(lstReturn != null);
		Test.stopTest();
	} 
}


Let me know if it solves your problem.
Thanks
niraj
Mohammed ArshadMohammed Arshad
Thanks for your prompt response  Niraj Kr Singh. Please find the complete Controller code below :

public with sharing class IS_Retailer{        
    public Decimal TotalCount;
    public IS_Retailer() {
    }

    public List<IS_Checker> check(IS_Retail retail) {
        List<IS_Checker> checkErrors = new List<IS_Checker>();
        if(retail.app.Total_Cash__c < 1000){
            IS_Checker ive = new IS_Checker('check Application', 'Cash is less than 1000');
            checkErrors.add(ive);
        }
        checkErrors.addAll(checkIsAvailable(retail.soapList1,'Atleast 1 ABC is required'));
        checkErrors.addAll(checkIsAvailable(retail.foamList1,'Atleast 1 XYZ is required'));
        if(retail.applicant2 != null){
            checkErrors.addAll(checkIsAvailable(retail.foamList2,'Atleast 1 ABC is required.'));
            checkErrors.addAll(checkIsAvailable(retail.soapList2,'Atleast 1 XYZ is required'));
        }
        Id appId = ApexPages.currentPage().getParameters().get('id');
        List<AggregateResult> groupedResults = [select Count(Application__r.Checker_Count__c) plan,SUM(No_of_Checker_Plans__c) aver from Checker__c where Application__c = :appId];
        Object chplan = groupedResults[0].get('plan');
        Object chCount = groupedResults[0].get('aver');               
        TotalCount = (integer)chplan + (Decimal)chCount;
       if(TotalCount > 30){
        IS_Checker ive = new IS_Checker('', 'Exceeded maximum number of allowed Checks (30)');
            checkErrors.add(ive);
        }
        if((retail.app.Sync__c != true && !retail.app.Code__c.contains('ERR')) && !retail.app.Docs__c){      
            checkErrors.add(new IS_Checker('','Application is updated.'));
        }
        return checkErrors; 
    }    


    public List<IS_Checker> checkIsAvailable(List<SObject> objList, String err) {
        List<IS_Checker> checkErrors = new List<IS_Checker>();
        
        if(objList != null){
            if(objList.isEmpty()){
               IS_Checker ive = new IS_Checker('check Application', err);
               checkErrors.add(ive);           
            }
        }
        return checkErrors;
    }
    
}