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
sgsssgss 

Test class on account contact

Can anyone help in Test Class of following problem.? Consider all positive negative scenario.

 SOQL query on Account and find all associated contact records of the Account which
contains the word 'John'. Print all Account and Contact records retrieved above

public class JohnClass {
    public static void getAccountContact(){
    List<Account> accountList = [SELECT 
                                                                        Id,
                                                                            Name, 
                                (SELECT 
                                                                        Id, 
                                                                            FirstName, 
                                                                                LastName
                                FROM 
                                                                    Contacts 
                                WHERE 
                                                                    FirstName LIKE '%john%' OR LastName LIKE '%john%') 
                                FROM 
                                                                    Account];
    
    for(Account account : accountList) {
        if(account.Contacts.isEmpty()) 
            continue;
            system.debug('Account : ' + account.Name);
        
        for(Contact contact : account.Contacts) {
            system.debug('Contact : ' + contact.FirstName + ' ' + contact.LastName);
        }
    }    
}
}
Niraj Kr SinghNiraj Kr Singh
Hi Supriya,

Plz try this test class:
@isTest
private class TestClass {

	@isTest static void myTestWithContact() {
		Account objAcc = new Account();
		objAcc.Name = 'Test Account';
		//Provide other required fields here.

		insert objAcc;

		
		Contact objCon = new Contact();
		objCon.FirstName= 'Test';
		objCon.LastName = ' john Contact';
		objCon.AccountId = objAcc.Id;
		//Provide other required fields here.

		insert objCon;
		
		Test.startTest();
			JohnClassJohnClass.getAccountContact();
		Test.stopTest();
	}

	@isTest static void myTestWithoutContact() {
		Account objAcc = new Account();
		objAcc.Name = 'Test Account';
		//Provide other required fields here.

		insert objAcc;

		/*
		Contact objCon = new Contact();
		objCon.FirstName= 'Test';
		objCon.LastName = ' john Contact'
		objCon.AccountId = objAcc.Id
		//Provide other required fields here.

		insert objCon;
		*/
		Test.startTest();
			JohnClassJohnClass.getAccountContact();
		Test.stopTest();
	}
}
Let me know if it works for you, and mark your ans.

Thanks
Niraj
 
Ajay K DubediAjay K Dubedi
Hi Supriya,

Its good to know that it works for you.
Below is the test class considering the negative and positive scenario.
 
@isTest
private class JohnClass_Test { 
      @isTest static void ContactsAccounts_Test() {
        List<Account> accList = new List<Account>();
        List<Contact> conList = new List<Contact>();
        
        for(Integer i=0; i<10 ; i++)
        {
            Account accObj = new Account();
            accObj.Name = 'abc' + i; 
            accList.add(accObj);
        }
        insert accList;
        
        set<Id> accId = new set<Id>();       
        for(Account accObj : accList)
        {
            accId.add(accObj.Id);
        }
        
       
            for(Integer i=0; i<5 ; i++)
            {
                Contact conObj = new Contact();
                
                conObj.FirstName = 'john' + i;
                conObj.LastName = 'john' + i;
                conObj.AccountId = accList[i].id;
                
                conList.Add(conObj);
            }
              
        
        insert conList;
        test.startTest();
        JohnClass.getAccountContact();         
        List<Contact> conLocalList = new List<Contact>([select id from Contact]);
        system.assertEquals(5, conLocalList.size()); 
        test.stopTest();
    }
    
     @isTest static void negativeContactsAccounts_Test() {
           List<Account> accList = new List<Account>();
        List<Contact> conList = new List<Contact>();
        
        for(Integer i=0; i<10 ; i++)
        {
            Account accObj = new Account();
            accObj.Name = 'bc' + i; 
            accList.add(accObj);
        }
        insert accList;
        
        set<Id> accId = new set<Id>();       
        for(Account accObj : accList)
        {
            accId.add(accObj.Id);
        }
        
        
            for(Integer i=0; i<5 ; i++)
            {
                Contact conObj = new Contact();
                
                conObj.FirstName = 'sagar' + i;
                conObj.LastName = 'goyal' + i;
                conObj.AccountId = accList[i].id;
                
                conList.Add(conObj);
            }
        
         insert conList;
        test.startTest();
        JohnClass.getAccountContact();         
        List<Contact> conLocalList = new List<Contact>([select id from Contact]);
        system.assertEquals(5, conLocalList.size());
        test.stopTest();
     } 
}

Hope this solution helps.

Thank You
Ajay Dubedi