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
Ravi kumar 292Ravi kumar 292 

How to cover the For loop and IF condition in Test class.

Hi all,

How to cover the for loop and if condition in test class. This is mode.

 for(i=0; i<c.Size();i++){
            if (c[i].Borrower_Type__c == 'Borrower'){
                Borrower.FirstName = c[i].FirstName;
                Borrower.LastName = c[i].LastName;
                Borrower.Birthdate = c[i].Birthdate;
                Borrower.Gender__c = c[i].Gender__c;
                Borrower.PAN_ID__c = c[i].PAN_ID__c;
                // Borrower.Age1__c =c[i].Age1__c;
                Borrower.phone =  c[i].phone;
                Borrower.Annual_Income_Gross__c = c[i].Annual_Income_Gross__c;
                Borrower.Marital_Status__c =c[i].Marital_Status__c;
                Borrower.Email = c[i].Email;
                Borrower.MobilePhone = c[i].MobilePhone;
                // Borrower.Sector__c = c[i].Sector__c;
                Borrower.MailingStreet = c[i].MailingStreet;
                Borrower.MailingCity = c[i].MailingCity;
                Borrower.MailingState = c[i].MailingState; 
                Borrower.MailingCountry = c[i].MailingCountry;   
                Borrower.MailingPostalCode = c[i].MailingPostalCode;
                Borrower.Residence_Type__c = c[i].Residence_Type__c;
                Borrower.Organisation__c = c[i].Organisation__c;
                
                date datetoday = system.today();
                Integer todayyr = datetoday.year(); 
                Integer birthyr = c[i].Birthdate.year();
                Age = (Todayyr - birthyr); 
                system.debug('Age :' + Age);
                dateInStringString = string.valueOfGmt(c[i].Birthdate);
                system.debug('dateInStringString :' +dateInStringString);
                MM = dateInStringString.substring(5, 7); 
                system.debug('MM :' +MM);
                DD = dateInStringString.substring(8, 10);
                system.debug('DD :' +DD);
                YYYY = dateInStringString.substring(0, 4);
                system.debug('YYYY :' +YYYY);
                // insert Borrower;
                // bankfacility = [Select Bank_Account_Number__c from Bank_Facilities__c where Id = :c[i].id limit 1];
            }
            else if (c[i].Borrower_Type__c == 'Co-Borrower'){
                CoBorrower.FirstName = c[i].FirstName;
                CoBorrower.LastName = c[i].LastName;
                CoBorrower.Birthdate = c[i].Birthdate;
                CoBorrower.Gender__c = c[i].Gender__c;
                CoBorrower.PAN_ID__c = c[i].PAN_ID__c;
                CoBorrower.phone = c[i].phone;
                CoBorrower.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
                CoBorrower.Marital_Status__c =c[i].Marital_Status__c;
                CoBorrower.Email =c[i].Email;
                CoBorrower.MobilePhone = c[i].MobilePhone;
                CoBorrower.MailingStreet = c[i].MailingStreet;
                CoBorrower.MailingCity = c[i].MailingCity;
                CoBorrower.MailingState = c[i].MailingState; 
                CoBorrower.MailingCountry = c[i].MailingCountry;   
                CoBorrower.MailingPostalCode = c[i].MailingPostalCode;
                CoBorrower.Residence_Type__c = c[i].Residence_Type__c;
                CoBorrower.Organisation__c = c[i].Organisation__c; 
                //insert CoBorrower;          
            }
            else if (c[i].Borrower_Type__c == 'Guarantor'){
                Guarantor.FirstName =c[i].FirstName;
                Guarantor.LastName = c[i].LastName;
                Guarantor.Birthdate =c[i].Birthdate;
                Guarantor.Gender__c = c[i].Gender__c;
                Guarantor.PAN_ID__c = c[i].PAN_ID__c;
                Guarantor.Phone =c[i].Phone;
                Guarantor.Annual_Income_Gross__c=c[i].Annual_Income_Gross__c;
                Guarantor.Marital_Status__c =c[i].Marital_Status__c;
                Guarantor.Email = c[i].Email;
                Guarantor.MobilePhone = c[i].MobilePhone;
                Guarantor.MailingStreet = c[i].MailingStreet;
                Guarantor.MailingCity = c[i].MailingCity;
                Guarantor.MailingState = c[i].MailingState; 
                Guarantor.MailingCountry = c[i].MailingCountry;   
                Guarantor.MailingPostalCode = c[i].MailingPostalCode;
                Guarantor.Residence_Type__c = c[i].Residence_Type__c;
                Guarantor.Organisation__c = c[i].Organisation__c;
                //  insert Guarantor;
            }
            else if(c[i].Borrower_Type__c =='Patient'){
                Patient.FirstName =c[i].FirstName;
                Patient.LastName =c[i].LastName;
                Patient.Birthdate =c[i].Birthdate;
                Patient.Gender__c = c[i].Gender__c;
                Patient.PAN_ID__c =c[i].PAN_ID__c;
                Patient.Phone =c[i].Phone;
                Patient.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
                Patient.Marital_Status__c =c[i].Marital_Status__c;
                Patient.Email = c[i].Email;
                Patient.MobilePhone =c[i].MobilePhone;
                Patient.MailingStreet = c[i].MailingStreet;
                Patient.MailingCity = c[i].MailingCity;
                Patient.MailingState = c[i].MailingState; 
                Patient.MailingCountry = c[i].MailingCountry;   
                Patient.MailingPostalCode = c[i].MailingPostalCode;
                Patient.Residence_Type__c = c[i].Residence_Type__c;
                Patient.Organisation__c = c[i].Organisation__c;
                //  insert Patient;
            }
            
        }

please help on this.

Thanks in advance..
Best Answer chosen by Ravi kumar 292
Mahesh DMahesh D
Hi Ravi,

Please find the below test class:
 
@isTest
public class TestHunterClass {
	@isTest
	public static void testHC() {
		Account acc = new Account(Name = 'Test Account');
        insert acc;
		
		List<Contact> conList = new List<Contact>();
		
		Contact conOne = new Contact(LastName = 'Test Contact LName One', AccountId = acc.Id, Borrower_Type__c == 'Borrower');
		conList.add(conOne);
		
		Contact conTwo = new Contact(LastName = 'Test Contact LName Two', AccountId = acc.Id, Borrower_Type__c == 'Co-Borrower');
		conList.add(conTwo);
		
		Contact conThree = new Contact(LastName = 'Test Contact LName Three', AccountId = acc.Id, Borrower_Type__c == 'Guarantor');
		conList.add(conThree);
		
		Contact conFour = new Contact(LastName = 'Test Contact LName Four', AccountId = acc.Id, Borrower_Type__c == 'Patient');
		conList.add(conFour);
		
		insert conList;
		
		Opportunity opp = new Opportunity(Name = 'Test Contact LName', AccountId = acc.Id);        
        opp.StageName = 'Identified';
        opp.CloseDate = Date.today();
        opp.Amount = 100;
		opp.Purpose_of_Loan__c = 'BUY FIRST HOUSE';
        insert opportunity;
		
		String oppId = opp.Id;
		HunterClass hc = HunterClass(oppId);
	}
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Ravi,

Please copy the full class and also copy the code using (Add a code sample <>) so that it will be easy to understand.

Regards,
Mahesh
Ravi kumar 292Ravi kumar 292
Hi Mahesh,

This is my total class.

Public class HunterClass{
    
    public void HunterClass(string rid){ 
        
        Opportunity o=[Select id,Name,AccountId,Nationality__c,Purpose_of_Loan__c,Sanction_Amount__c,Application_Creation_Date__c,Original_Face__c,Loan_Tenor_in_Month__c,Other_Loan_Purpose__c from Opportunity where id= :rid];
        system.debug('print me account id'+o.AccountId);
        Account a=[Select Id,Loan_Amount__c, Sector__c from Account where id = :o.AccountId];
        system.debug('Print me account id'+a.id);
        List<Contact> c=[Select Id,FirstName,LastName,Birthdate,Borrower_Type__c,Name_of_Firm_Company_Establishment_In__c,Gender__c,Age1__c,PAN_ID__c,Residence_Type__c, Phone, Annual_Income_Gross__c,Organisation__c,MailingStreet, MailingCity,MailingState, MailingCountry,MailingPostalCode,Marital_Status__c,Email,MobilePhone From Contact where AccountId = :a.Id]; 
        system.debug('Contact' + c);
        contact Borrower = new Contact();
        contact Coborrower= new Contact();
        contact Guarantor = new Contact();
        contact Patient = new Contact();
        String dateInStringString;
        String dateInStringString1;
        String MM;
        String DD;
        String YYYY;
        String AMM;
        String ADD;
        String AYYYY;
        String result = '';
        Integer i;
        Integer Age;
        Bank_Facilities__c bankfacility;
        system.debug('bankfacility :' +bankfacility);
        
        
        dateInStringString1 = string.valueOfGmt(o.Application_Creation_Date__c);
        AMM = dateInStringString1.substring(5, 7); 
        system.debug('AMM :' +AMM);
        ADD = dateInStringString1.substring(8, 10);
        system.debug('ADD :' +ADD);
        AYYYY = dateInStringString1.substring(0, 4);
        system.debug('AYYYY :' +AYYYY);
        
        
        if(o.Purpose_of_Loan__c== 'BUY FIRST HOUSE' || o.Purpose_of_Loan__c == 'LOCAL PURCHASE' || o.Purpose_of_Loan__c == 'BUY SMALL FARM' ||
           o.Purpose_of_Loan__c == 'BUILDING' || o.Purpose_of_Loan__c == 'REFURBISHMENT PROPERTY' || o.Purpose_of_Loan__c == 'SUBROGATION' ||
           o.Purpose_of_Loan__c == 'REST' || o.Purpose_of_Loan__c == 'NO INFORMAATION' || o.Purpose_of_Loan__c == 'CREDIT CARD')
        {
            o.Purpose_of_Loan__c = 'HOUSING LOAN';
        } else
            
            o.Purpose_of_Loan__c = 'OTHER PURCHASE';
        
        
        for(i=0; i<c.Size();i++){
            if (c[i].Borrower_Type__c == 'Borrower'){
                Borrower.FirstName = c[i].FirstName;
                Borrower.LastName = c[i].LastName;
                Borrower.Birthdate = c[i].Birthdate;
                Borrower.Gender__c = c[i].Gender__c;
                Borrower.PAN_ID__c = c[i].PAN_ID__c;
                // Borrower.Age1__c =c[i].Age1__c;
                Borrower.phone =  c[i].phone;
                Borrower.Annual_Income_Gross__c = c[i].Annual_Income_Gross__c;
                Borrower.Marital_Status__c =c[i].Marital_Status__c;
                Borrower.Email = c[i].Email;
                Borrower.MobilePhone = c[i].MobilePhone;
                // Borrower.Sector__c = c[i].Sector__c;
                Borrower.MailingStreet = c[i].MailingStreet;
                Borrower.MailingCity = c[i].MailingCity;
                Borrower.MailingState = c[i].MailingState; 
                Borrower.MailingCountry = c[i].MailingCountry;   
                Borrower.MailingPostalCode = c[i].MailingPostalCode;
                Borrower.Residence_Type__c = c[i].Residence_Type__c;
                Borrower.Organisation__c = c[i].Organisation__c;
                
                date datetoday = system.today();
                Integer todayyr = datetoday.year(); 
                Integer birthyr = c[i].Birthdate.year();
                Age = (Todayyr - birthyr); 
                system.debug('Age :' + Age);
                dateInStringString = string.valueOfGmt(c[i].Birthdate);
                system.debug('dateInStringString :' +dateInStringString);
                MM = dateInStringString.substring(5, 7); 
                system.debug('MM :' +MM);
                DD = dateInStringString.substring(8, 10);
                system.debug('DD :' +DD);
                YYYY = dateInStringString.substring(0, 4);
                system.debug('YYYY :' +YYYY);
                // insert Borrower;
                // bankfacility = [Select Bank_Account_Number__c from Bank_Facilities__c where Id = :c[i].id limit 1];
            }
            else if (c[i].Borrower_Type__c == 'Co-Borrower'){
                CoBorrower.FirstName = c[i].FirstName;
                CoBorrower.LastName = c[i].LastName;
                CoBorrower.Birthdate = c[i].Birthdate;
                CoBorrower.Gender__c = c[i].Gender__c;
                CoBorrower.PAN_ID__c = c[i].PAN_ID__c;
                CoBorrower.phone = c[i].phone;
                CoBorrower.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
                CoBorrower.Marital_Status__c =c[i].Marital_Status__c;
                CoBorrower.Email =c[i].Email;
                CoBorrower.MobilePhone = c[i].MobilePhone;
                CoBorrower.MailingStreet = c[i].MailingStreet;
                CoBorrower.MailingCity = c[i].MailingCity;
                CoBorrower.MailingState = c[i].MailingState; 
                CoBorrower.MailingCountry = c[i].MailingCountry;   
                CoBorrower.MailingPostalCode = c[i].MailingPostalCode;
                CoBorrower.Residence_Type__c = c[i].Residence_Type__c;
                CoBorrower.Organisation__c = c[i].Organisation__c; 
                //insert CoBorrower;          
            }
            else if (c[i].Borrower_Type__c == 'Guarantor'){
                Guarantor.FirstName =c[i].FirstName;
                Guarantor.LastName = c[i].LastName;
                Guarantor.Birthdate =c[i].Birthdate;
                Guarantor.Gender__c = c[i].Gender__c;
                Guarantor.PAN_ID__c = c[i].PAN_ID__c;
                Guarantor.Phone =c[i].Phone;
                Guarantor.Annual_Income_Gross__c=c[i].Annual_Income_Gross__c;
                Guarantor.Marital_Status__c =c[i].Marital_Status__c;
                Guarantor.Email = c[i].Email;
                Guarantor.MobilePhone = c[i].MobilePhone;
                Guarantor.MailingStreet = c[i].MailingStreet;
                Guarantor.MailingCity = c[i].MailingCity;
                Guarantor.MailingState = c[i].MailingState; 
                Guarantor.MailingCountry = c[i].MailingCountry;   
                Guarantor.MailingPostalCode = c[i].MailingPostalCode;
                Guarantor.Residence_Type__c = c[i].Residence_Type__c;
                Guarantor.Organisation__c = c[i].Organisation__c;
                //  insert Guarantor;
            }
            else if(c[i].Borrower_Type__c =='Patient'){
                Patient.FirstName =c[i].FirstName;
                Patient.LastName =c[i].LastName;
                Patient.Birthdate =c[i].Birthdate;
                Patient.Gender__c = c[i].Gender__c;
                Patient.PAN_ID__c =c[i].PAN_ID__c;
                Patient.Phone =c[i].Phone;
                Patient.Annual_Income_Gross__c =c[i].Annual_Income_Gross__c;
                Patient.Marital_Status__c =c[i].Marital_Status__c;
                Patient.Email = c[i].Email;
                Patient.MobilePhone =c[i].MobilePhone;
                Patient.MailingStreet = c[i].MailingStreet;
                Patient.MailingCity = c[i].MailingCity;
                Patient.MailingState = c[i].MailingState; 
                Patient.MailingCountry = c[i].MailingCountry;   
                Patient.MailingPostalCode = c[i].MailingPostalCode;
                Patient.Residence_Type__c = c[i].Residence_Type__c;
                Patient.Organisation__c = c[i].Organisation__c;
                //  insert Patient;
            }
            
        }
   
        string controlXml = 'Some String';
        string batchXml = 'some String';
        
        string username = 'username';
        string password = 'password';    
        wwwMclsoftwareCoUkHunteriiWebservic.MatchingSoap  c1 = new wwwMclsoftwareCoUkHunteriiWebservic.MatchingSoap(); 
        string response_x = c1.Match(controlXml, batchXml, username, password);
        
        
        DOM.Document xmlDOC = new DOM.Document();
        xmlDOC.load(response_x);
        DOM.XMLNode rootElement = xmlDOC.getRootElement();
        // System.Debug('Root Element :: '+rootelement);
        for(DOM.XMLNode xmlNodeObj : xmlDOC.getRootElement().getChildElements()){  
            // System.Debug('Child Elements :: '+xmlNodeObj);
           /* for(DOM.XMLNode xmlNodeObjChild:xmlNodeObj.getChildren())
            {
                // System.Debug('$$$ Childrens'+xmlNodeObjChild.getChildren());
                
                if(xmlNodeObjChild.getName()=='MatchSchemes')
                    System.Debug('-----------' + xmlNodeObjChild.getText());
                if(xmlNodeObjChild.getName()=='Scheme')
                    System.Debug('-----------' + xmlNodeObjChild.getText());
                if(xmlNodeObjChild.getName()=='SchemeID')
                    System.Debug('-----------' + xmlNodeObjChild.getText());
                if(xmlNodeObjChild.getName()=='Score')
                    System.Debug('-----------' + xmlNodeObjChild.getText());
            }*/
            if (xmlNodeObj.getAttributeCount() > 0) {
                for (Integer j = 0; j< xmlNodeObj.getAttributeCount(); j++ ) {
                    result += xmlNodeObj.getAttributeValue(xmlNodeObj.getAttributeKeyAt(j), xmlNodeObj.getAttributeKeyNsAt(j));
                    system.debug('Result : '+ result);
                } 
            }    
        }
        
        Multibureau_Data__c mbd = new Multibureau_Data__c();
        mbd.Bureau__c = 'HUNTER';
        mbd.Response__c = response_x;
        if(result == '1'){
            mbd.Flag__c = 'Match';
        }else
            mbd.Flag__c = 'No Match';
        system.debug('Flag : '+mbd.Flag__c);
        
        insert mbd;
        
        //  return null;  
        
    }
}
Mahesh DMahesh D
Hi Ravi,

Please find the below test class:
 
@isTest
public class TestHunterClass {
	@isTest
	public static void testHC() {
		Account acc = new Account(Name = 'Test Account');
        insert acc;
		
		List<Contact> conList = new List<Contact>();
		
		Contact conOne = new Contact(LastName = 'Test Contact LName One', AccountId = acc.Id, Borrower_Type__c == 'Borrower');
		conList.add(conOne);
		
		Contact conTwo = new Contact(LastName = 'Test Contact LName Two', AccountId = acc.Id, Borrower_Type__c == 'Co-Borrower');
		conList.add(conTwo);
		
		Contact conThree = new Contact(LastName = 'Test Contact LName Three', AccountId = acc.Id, Borrower_Type__c == 'Guarantor');
		conList.add(conThree);
		
		Contact conFour = new Contact(LastName = 'Test Contact LName Four', AccountId = acc.Id, Borrower_Type__c == 'Patient');
		conList.add(conFour);
		
		insert conList;
		
		Opportunity opp = new Opportunity(Name = 'Test Contact LName', AccountId = acc.Id);        
        opp.StageName = 'Identified';
        opp.CloseDate = Date.today();
        opp.Amount = 100;
		opp.Purpose_of_Loan__c = 'BUY FIRST HOUSE';
        insert opportunity;
		
		String oppId = opp.Id;
		HunterClass hc = HunterClass(oppId);
	}
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Ravi kumar 292Ravi kumar 292
Thanks Mahesh..