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
Sana123Sana123 

I am new to salesforce . How can i write test classes for this code. Can someone please help me

public class SOQLquery {

  public static void searchForAccount (){
      
       List<Account> acctOfList = [SELECT Name,Website,Offer__c FROM Account ] ;
      System.debug(acctOfList);
     
       List<Account> acctOfList1 = [SELECT Name,Website,Offer__c FROM Account WHERE Name = 'Test' OR Name ='Testing'] ;
      System.debug(acctOfList1);
        
       List<Account> notAccts = [SELECT Name,Website,Offer__c FROM Account WHERE (Name != 'Test' OR Name !='Testing') AND Website != Null ] ;
      System.debug(notAccts);   
    }
    
    /**
     * 
     *   @description    :   Description of the childToParentSOQL
     *
     *   @args           :   
     *
     *   @return         :   void
     *
     *   @revision Log   :   V1.0 - Created  - 2021/06/22 - Anjana Sharma 
     *             
     * 
     **/
    public static void childToParentSOQL(){
        
       List<Contact> objContact =[SELECT Id, FirstName , LastName, Email,Phone, Account.Id, Account.Name , Account.Website FROM Contact] ;
      System.debug(objContact);
       
        List<Contact> objContacts =[SELECT Id, FirstName , LastName, Email,Phone, Account.Id, Account.Name , Account.Website FROM Contact WHERE Email != Null OR Phone != Null] ;
      System.debug(objContacts);
        
        List<Contact> listOfAccount =[SELECT Id, FirstName , LastName, Email,Phone, Account.Id, Account.Name , Account.Website FROM Contact WHERE Account.Id != Null OR Account.Name != 'Test'] ;
      System.debug(listOfAccount);
        
         List<Contact> listOfAccount1 =[SELECT Id, FirstName , LastName, Email,Phone, Account.Id, Account.Name , Account.Website FROM Contact WHERE LastName != Null OR Account.Name != 'Testing'] ;
      System.debug(listOfAccount1);
     
    }
    
  
   public static void parentToChildSOQL(){
        List<Account> accountList =[SELECT Id,Name, (SELECT Id, Name FROM Account_Contact_Childs__r) FROM Account];
         for(Account account : accountList){
        System.debug(account);
         }
         
        List<Contact> contactList =[SELECT Id, (SELECT Id, Name FROM Account_Contact_Childs__r) FROM Contact];
       for(Contact contact : contactList){
        System.debug(contact);
         }
     
    }
      
}

  public static Map<Id, List<Contact>> getContactInformationUsingInlineSOQL(){
        
        Map<Id, List<Contact>> contactMap = new Map<Id, List<Contact>>(); 
       List<Account> lstContact =[SELECT Id ,(SELECT Name FROM Contacts) FROM Account];         
        
        for(Account objContact : lstContact ){   
            contactMap.put(objContact.id, objContact.Contacts);
           System.debug(contactMap);  
        }     
        return contactMap;   
    }

 public static Map<Id, List<Contact>> getContactInformationUsingSOQL(){
     
       List<Account> lstAccount =[SELECT Id FROM Account ];
        List<Contact> lstContact =[SELECT Id , Name FROM Contact WHERE AccountID IN :lstAccount];         
        Map<Id, List<Contact>> contactMap = new Map<Id, List<Contact>>(); 
        for(Contact objContact : lstContact ){
            
            contactMap.put(objContact.Id, new List<Contact>{objContact});
           System.debug(contactMap);  
        }     
        return contactMap;   
    }

 public static Map<Id, List<Contact>> getContactInformationForSpecificAccount(List<Account> acct){
         
         List<Account> lstOfAccount = [SELECT Name FROM Account WHERE ID IN:acct];
         List<Contact> lstOfContact = [SELECT Name FROM Contact WHERE AccountID IN :lstOfAccount]; 
         Map<Id, List<Contact>> contactMap1 = new Map<Id, List<Contact>>(); 
         
         for(Contact objContact : lstOfContact ){
             contactMap1.put(objContact.ID, new List<Contact>{objContact});
           System.debug(contactMap1);  
        }     
        return contactMap1;  
     }
    
Best Answer chosen by Sana123
Suraj Tripathi 47Suraj Tripathi 47

Hi Anu,

Try this code:

@isTest
public class TestClass_YA {
    static testmethod void youngtestMethod(){
        Account ac = new Account();
        ac.Name='Test';
        ac.Website='sdxcvbnm';
        ac.Offer__c='szdxfcbnm';
        insert ac;
        List<Account> acList = new List<Account>();
        acList.add(ac);
        
        Contact con = new Contact();
        con.firstName='xdfchjbk';
        con.lastname='lkjhgf';
        con.Email='swati.k@gm.com';
        con.Phone='6789887767';
        con.AccountId=ac.id;
        insert con;
        
        
        
        test.startTest();
        SOQLquery.method1();
        SOQLquery.childToParentSOQL();
        SOQLquery.getContactInformationUsingInlineSOQL();
        SOQLquery.getContactInformationUsingSOQL();
        SOQLquery.getContactInformationForSpecificAccount(acList);
        SOQLquery.parentToChildSOQL();
        test.stopTest();
        
   
      
     
 }
   
    
}

Please Mark it as Best Answer if it helps.

Thanks

All Answers

CharuDuttCharuDutt
Hii Anu
Try Below Test Class
Made Small Change In Your Class
Which Is In Bold And Underlined
 public static void parentToChildSOQL(){
         List<Account> accountList =[SELECT Id,Name, (SELECT Id, Name FROM Contacts) FROM Account];
         for(Account account : accountList){
        System.debug(account);
         }
         
        List<Contact> contactList =[SELECT Id,Account.Name FROM Contact];
       for(Contact contact : contactList){
        System.debug(contact);
         }
     
    }
      
}



@isTest
public class SOQLqueryTest {
@isTest
    public Static void UnitTest(){
        Account Acc =  new Account();
        Acc.Name = 'Test';
        Acc.Website ='WWW.Google.Com';
        Acc.Offer__c = /*Fill Up*/;
        Insert Acc;
        list<Account> lstAcc= new list<Account>();
        lstAcc.Add(Acc);
        Contact Con = new Contact();
        Con.LastName = 'Test Con1';
        Con.Email = 'Test@test.com';
        Con.Phone = '0123456789';
        Con.AccountId = Acc.id;
        Insert Con;
        
        Account Acc1 =  new Account();
        Acc1.Name = 'Tester';
        Acc1.Website ='WWW.Google.Com';
        Acc.Offer__c = /*Fill Up*/;
        Insert Acc1;
        Contact Con1 = new Contact();
        Con1.LastName = 'Test Con';
        Con1.Email = 'Test1@test.com';
        Con1.Phone = '0123456789';
        Con1.AccountId = Acc1.id;
        Insert Con1;
        
        SOQLquery.searchForAccount();
        SOQLquery.childToParentSOQL();
        SOQLquery.parentToChildSOQL();
        SOQLquery.getContactInformationUsingSOQL();
        SOQLquery.getContactInformationForSpecificAccount(lstAcc);
        SOQLquery.getContactInformationUsingInlineSOQL();
    }
}
Please Mark it As Best Answer If It Helps
Thank You!

 
Suraj Tripathi 47Suraj Tripathi 47

Hi Anu,

Try this code:

@isTest
public class TestClass_YA {
    static testmethod void youngtestMethod(){
        Account ac = new Account();
        ac.Name='Test';
        ac.Website='sdxcvbnm';
        ac.Offer__c='szdxfcbnm';
        insert ac;
        List<Account> acList = new List<Account>();
        acList.add(ac);
        
        Contact con = new Contact();
        con.firstName='xdfchjbk';
        con.lastname='lkjhgf';
        con.Email='swati.k@gm.com';
        con.Phone='6789887767';
        con.AccountId=ac.id;
        insert con;
        
        
        
        test.startTest();
        SOQLquery.method1();
        SOQLquery.childToParentSOQL();
        SOQLquery.getContactInformationUsingInlineSOQL();
        SOQLquery.getContactInformationUsingSOQL();
        SOQLquery.getContactInformationForSpecificAccount(acList);
        SOQLquery.parentToChildSOQL();
        test.stopTest();
        
   
      
     
 }
   
    
}

Please Mark it as Best Answer if it helps.

Thanks

This was selected as the best answer
Sana123Sana123
Actually i have not much idea about test classes could you please explain me that how can i use it and what this test class is working.