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
KimKim 

Help with test class code coverage

Hello,  

I'm new at writing apex class and test class. I've written a class that's updating a SubMarket field in an account from a Custom Setting. I've created a test class to test if the SubMarket field value on the account matches the value from the SubMarket in the custom setting. I'm only getting 69% code coverage. I'm not sure what I'm missing/need to add to increase my code coverage. Any feedback would be greatly appreciated. 

Apex class: 
public class AccountSubMarketUpdate {
    public static void setSubMarket(List<Account> acctList){
  
        Set<String> billingCountries = new Set<String>(); 
     
        List<Account> finalAccount = new List<Account>();
        for( Account a : acctList){
            if(String.isBlank(a.SubMarket__c)){
               billingCountries.add(a.BillingCountry); 
               finalAccount.add(a);
            }
        }
           Map<String,CountryMarketMatchMap__c> marketName = CountryMarketMatchMap__c.getAll(); 
        Map<String,String> finalsubMarket = new Map<String,String>(); 
        for (CountryMarketMatchMap__c m : marketName.values()){
               finalsubMarket.put(m.Name,m.Sub_Market__c);
                  }
        for(Account a: finalAccount){
            a.SubMarket__c  = finalsubMarket.get(a.BillingCountry); 
           }
        }
}
   
TestClass: 
@isTest
public class AccountSubMarketUpdateTest {
    @testSetup static void setupTests(){
        List<TriggerController__c> tc = new List<TriggerController__c>{
            new TriggerController__c(Name='Account', Disabled__c=false)
        };
            insert tc; 
    }
    
    static testMethod void testSubMarketUpdate(){
        List<Account> newAcctList = new List<Account>();
        
        Account a1 = new Account();
        a1.name = 'AcctSubMarket';
        a1.BillingCountry = 'Albania';
        a1.SubMarket__c = 'ROE';
        
        Account a2 = new Account();
        a2.name = 'AcctNoMatchSubMarket'; 
        a2.BillingCOuntry = 'Albania';
        a2.SubMarket__c = 'ROE'; 
        
        Test.startTest();
        insert(a1); 
        insert(a2); 
        Test.stopTest();
        
   //Test1: Acct SubMarket matches CS SubMarket.   
        CountryMarketMatchMap__c request = new CountryMarketMatchMap__c();
        request.Sub_Market__c ='ROE';
        request.Name ='Albania'; 
   
        Account returnMarket = [SELECT Id,SubMarket__c,BillingCountry FROM Account WHERE Id =:a1.Id LIMIT 1];
        System.assertEquals(request.Name, returnMarket.BillingCountry);
        System.assertEquals(request.Sub_Market__c, returnMarket.SubMarket__c);
        
   //Test2: Acct SubMarket does not match CS SubMarket. 
           CountryMarketMatchMap__c request2 = new CountryMarketMatchMap__c();
        request2.Sub_Market__c = 'Africa';
        request2.Name = 'Albania'; 
        
        Account noMatchMarket = [SELECT Id,SubMarket__c,BillingCountry FROM Account WHERE Id =:a2.Id LIMIT 1];
        System.assertNotEquals(noMatchMarket.SubMarket__c, 'Africa');
    
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class AccountSubMarketUpdateTest 
{
    static testMethod void testSubMarketUpdate()
	{
		
		List<TriggerController__c> tc = new List<TriggerController__c>{
            new TriggerController__c(Name='Account', Disabled__c=false)
        };
        insert tc; 

        CountryMarketMatchMap__c request = new CountryMarketMatchMap__c();
        request.Sub_Market__c ='ROE';
        request.Name ='Albania'; 
		insert request
        
		List<Account> lstAccount = new List<Account>();
		
        Account a1 = new Account();
        a1.name = 'AcctSubMarket';
        a1.BillingCountry = 'Albania';
        
        Account a2 = new Account();
        a2.name = 'AcctNoMatchSubMarket'; 
        a2.BillingCOuntry = 'Albania';
        a2.SubMarket__c = 'ROE'; 
        
		lstAccount.add(a1);
		lstAccount.add(a2);
		
        Test.startTest();
		
			insert lstAccount;
			
			// uncomment below code if we are not calling AccountSubMarketUpdate  class from trigger
			
			/*
			
				AccountSubMarketUpdate.setSubMarket(lstAccount);
			*/
		
        Test.stopTest();
        
		/*
			Account returnMarket = [SELECT Id,SubMarket__c,BillingCountry FROM Account WHERE Id =:a1.Id LIMIT 1];
			System.assertEquals(request.Name, returnMarket.BillingCountry);
			System.assertEquals(request.Sub_Market__c, returnMarket.SubMarket__c);
		*/
		
    }
}

Let us know if this will help you