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
BsinBsin 

testclass for a class with more than one method

Hi,

Could some one please suggest me how to write a test class for  a class having more than one method.

Also i have provided the input for record update in test class.But code to executed for before update scenario is not covered by the test class. Could some one guide me what could be the reason.

Thanks & Regards,
BSIN
salesforcelearner1salesforcelearner1
Hi Bsin,
Can you share me your code. So that I can tell you.

Thanks&Regards,
Salesforcelearner
BsinBsin
Can you please guide me how to write a  test class for a class having more than one method? whether we should write one test method for each method used in class or one test method with all the input ?
salesforcelearner1salesforcelearner1
Hi Bsin,

You can write one test method for 'n' number of methods  in apex class. If you have insert/update operations. create record with test data and call the methods in test.
BsinBsin
ok sure. let me give a try.Thank you.
Ajay K DubediAjay K Dubedi
Hi Bsin,
        You Need to create test methods for each for those methods of your actual class methods.
        Hope the below example helps you
        
        public class Example {
    
     public static void variousExample()
    {
        
       Map<Id,List<Contact>> accountidVsContactList = new Map<Id,List<Contact>>();
       List<Contact> contactList = new List<Contact>();
       contactList = [SELECT Id,AccountId FROM Contact WHERE AccountId != null LIMIT 10000 ];
       
       if(contactList.size() > 0 ){
           
           for(Contact conObj : contactList){
               if(!accountidVsContactList.containsKey(conObj.AccountId)){
                   accountidVsContactList.put(conObj.AccountId, new List<Contact>());
               }
                   accountidVsContactList.get(conObj.AccountId).add(conObj);
           }
           
       }
       system.debug(accountidVsContactList);
    }
    
    public static void AccNamesInOrder()
    {
        List<Account> Acc = new List<Account>();        
        Acc = [select Name from Account order by Name];
        
        system.debug('Account Name in Order:');        
        if(Acc != null && Acc.size() > 0)
        {
            for(Account accObj : Acc)
            {   
                system.debug(accObj);
            }
        }
    }
}




@isTest
private class Example_Test {
    
    @isTest static void variousExample_Test()
    {  
        List<Account> accList = new List<Account>();
        for(Integer i= 0; i < 10 ; i++)
        {
            Account accObj = new Account();
            accObj.Name = 'accName';
            accList.add(accObj);     
        }
        insert accList;
        
        
        List<contact> conList = new List<Contact>();
        for(Integer i= 0; i < 10 ; i++)
        {
            contact conObj = new contact();
            conObj.LastName = 'conLastName';
            conObj.AccountId = accList[i].id;
            conList.add(conObj);     
        }
        insert conList;
        
        test.startTest();
        Example.variousExample();
        List<Contact> conNewList = new List<Contact>();
        conNewList = [SELECT id, Name FROM Contact WHERE LastName = 'conLastName'];
        system.assertEquals(10, conNewList.size());
        test.stopTest();
    }
    
    
    @isTest static void variousExample_Negative_Test()
    {  
        List<Account> accList = new List<Account>();
        for(Integer i= 0; i < 10 ; i++)
        {
            Account accObj = new Account();
            accObj.Name = 'accName';
            accList.add(accObj);     
        }
        insert accList;
        
        
        List<contact> conList = new List<Contact>();
        for(Integer i= 0; i < 10 ; i++)
        {
            contact conObj = new contact();
            conObj.LastName = 'conLastName';
            conList.add(conObj);     
        }
        insert conList;
        
        test.startTest();
        Example.variousExample();
        List<Contact> conNewList = new List<Contact>();
        conNewList = [SELECT id, Name FROM Contact WHERE AccountId != null];
        system.assertEquals(0, conNewList.size());
        test.stopTest();        
    }
    
    @isTest static void namesInOrder_Test() {
        List<Account> AccList = new List<Account>();
        
        for(Integer i=0; i<5 ; i++)
        { 
            Account accObj = new Account();
            if(i==0)
                accObj.Name = 'Amit';
            else if(i==1)
                accObj.Name = 'Bali';
            else if(i==2)
                accObj.Name = 'Carl';
            else if(i==3)
                accObj.Name = 'Dent';
            else
                accObj.Name = 'Eagle';
            AccList.add(accObj);
        }       
        insert AccList; 
        
        
        test.startTest();
        Example.AccNamesInOrder();     
        test.stopTest();
    }
    
    @isTest static void negative_Test() {
        test.startTest();
        Example.AccNamesInOrder();     
        test.stopTest();
    } 
}

Thank You
Ajay Dubedi
salesforcelearner1salesforcelearner1
Hi Bsin,
Did you complete the test class?.