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
sathishsfdcsathishsfdc 

increase code coverage

I am writing a test class for a main class that will insert a value into a transaction table based on updates to specific fields in the account object.
I have written the class but i am not able to increase the test coverage.
any help will be appreciated.
public class TX_RecordUpdate {
    
    static Set<String> accfields; // Account fields to be monitored
    
    static {
        
        List<TX__c> customsettingfields = [select Name from TX__c  where Contact__c  = true];////field value getting from custom setting
                accfields = new Set<String>();
        for(TX__c customsettingfield : customsettingfields) {
            accfields.add(customsettingfield.name); // adding the listof fields
            system.debug('accfields will give' + Email, FirstName, LastName, MailingCity);
            
        }
        
        
    }
    
    public static void checkAccount(List<Account> accountList, Map<Id, Account> oldAccounts) {

        List<TX__c> insertevents = new List <TX__c> ();
        for(Account acc : accountList){
            
            Account oldacc = oldAccounts.get(acc.Id);
            for(String acckey : accfields) {  ///// test code coverage not going into this for loop
                if(acc.get(acckey) != oldacc.get(acckey)) {
                    insertevents.add(new TX__c (Action__c  = 'Success');
                    
                }//////
            }
        }
        if(!insertevents.isEmpty()) {
            try {
                insert insertevents;
            } catch (Exception Ex){
            }
        }
        
    }

    }


Test class:



	@isTest(seeAllData=true)

private class Test{

    private static testMethod void myTest() {
       
	   Account testAccount = new Account(Name='test');     
           insert testAccount;                                   
        
       Contact testContact = new Contact();
            testContact.LastName='testName';
            testContact.email = 'testname@gmail.com';
            testContact.AccountId = testAccount.id; 
              insert testContact; 
        
          
			TX__c cs = new TX__c (Name='Billing',Field__c= True);///inserting custom setting data
            insert cs ;    
            
			Account testAccount2 = new Account(Id=testAccount.Id);
           update testAccount2;
                
         
            Map<Id, Account> oldMap = new Map<Id, Account>{testAccount.Id => testAccount};
 
        
              Test.starttest();
              TX_RecordUpdate.checkAccount(new List<Account>{ testAccount2 }, oldMap);
              Test.stoptest();
          }

}
Best Answer chosen by sathishsfdc
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope that will help you
@isTest
private class Test
{
    private static testMethod void myTest() 
	{
       
	    List<Account> lstAccount = new List<Account>();
		Account testAccount = new Account(Name='test');     
        insert testAccount;                                   
        
		Contact testContact = new Contact();
		testContact.LastName='testName';
		testContact.email = 'testname@gmail.com';
		testContact.AccountId = testAccount.id; 
		insert testContact; 
        
		TX__c cs = new TX__c (Name='Billing',Field__c= True);///inserting custom setting data
		insert cs ;    
            
		Account testAccount2 = new Account(Id=testAccount.Id);
		update testAccount2;
        
		lstAccount.add(testAccount);
		Map<Id, Account> oldMap = new Map<Id, Account>{testAccount.Id => testAccount};
		
              Test.starttest();
			  TX_RecordUpdate	obj = new TX_RecordUpdate();
			  
              TX_RecordUpdate.checkAccount(lstAccount , oldMap);
              Test.stoptest();
    }

}
NOTE:- Please click on Run Test button and then check code coverage
 

All Answers

Arun KumarArun Kumar
Hi,

Give a try by calling constructor in your test class code:

TX_RecordUpdate newObj = new TX_RecordUpdate();
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope that will help you
@isTest
private class Test
{
    private static testMethod void myTest() 
	{
       
	    List<Account> lstAccount = new List<Account>();
		Account testAccount = new Account(Name='test');     
        insert testAccount;                                   
        
		Contact testContact = new Contact();
		testContact.LastName='testName';
		testContact.email = 'testname@gmail.com';
		testContact.AccountId = testAccount.id; 
		insert testContact; 
        
		TX__c cs = new TX__c (Name='Billing',Field__c= True);///inserting custom setting data
		insert cs ;    
            
		Account testAccount2 = new Account(Id=testAccount.Id);
		update testAccount2;
        
		lstAccount.add(testAccount);
		Map<Id, Account> oldMap = new Map<Id, Account>{testAccount.Id => testAccount};
		
              Test.starttest();
			  TX_RecordUpdate	obj = new TX_RecordUpdate();
			  
              TX_RecordUpdate.checkAccount(lstAccount , oldMap);
              Test.stoptest();
    }

}
NOTE:- Please click on Run Test button and then check code coverage
 
This was selected as the best answer