You need to sign in to do that
Don't have an account?
Kim
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');
}
}
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');
}
}
Let us know if this will help you