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
Dman100Dman100 

cover ApexPages.Message in unit test

I'm writing my unit test code for my custom controller and I've gotten to 70%, but I'm missing coverage on my three sections of my class file that include ApexPages.Message.  Can anyone advise on how I can cover this code?

 

Here is my controller class:

 

public class AccountMaintenanceController { private Account acctFrom = new Account(); private Account acctTo = new Account(); private Account acctToByParent = new Account(); private Account parentAcct = new Account(); private Account acctToByState = new Account(); private Account state = new Account(); public AccountMaintenanceController() { } public PageReference ChildAccountOwnershipCount() { Account parent = [Select Id, OwnerId From Account where Id =: parentAcct.ParentId]; ChildAccountCount = [Select Count() From Account where ParentId =: parent.Id]; ChildAccountCount += ChildAccountCount + 1; lblChildAccounts = true; btnChildAccounts = true; return null; } public PageReference updateChildAccountOwnership() { try { Account parent = [Select Id, OwnerId From Account where Id =: parentAcct.ParentId]; List<Account> childAccts = [Select Id, OwnerId From Account where ParentId =: parent.Id]; parent.ownerId = acctToByParent.ownerId; DbUtil.save(parent); List<Account> lst = new List<Account>(); for (Account acct: childAccts) { acct.OwnerId = acctToByParent.OwnerId; lst.add(acct); } DbUtil.save(lst); ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); ApexPages.addMessage(msg); acctToByParent.OwnerId = null; parentAcct.parentId = null; lblChildAccounts = false; btnChildAccounts = false; return null; } catch (Exception ex) { ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage()); ApexPages.addMessage(errMsg); return null; } } public PageReference StateAccountOwnershipCount() { StateAccountCount = [Select Count() From Account where state__c =: state.state__c]; lblStateAccounts = true; btnStateAccounts = true; return null; } public PageReference AccountOpportunityOwnershipCount() { if (acctChkbox) { AccountCount = [Select Count() From Account where ownerId =: acctFrom.ownerId]; } else { AccountCount = 0; } if (oppyChkbox) { OpportunityCount = [Select Count() From Opportunity where ownerId =: acctFrom.ownerId AND stagename <> 'Closed Lost']; } else { OpportunityCount = 0; } if (oppyEmailChkbox) { CommissionSalespersonCount = [Select Count() From Opportunity where ownerId =: acctFrom.ownerId AND stagename <> 'Closed Lost']; } else { CommissionSalespersonCount = 0; } lblAccountsOpportunities = true; btnAccountsOpportunities = true; return null; } public PageReference updateAccountByState() { try { List<Account> accts = [Select Id, OwnerId From Account where state__c =: state.state__c]; List<Account> lst = new List<Account>(); for (Account acct: accts) { acct.OwnerId = acctToByState.OwnerId; lst.add(acct); } DbUtil.save(lst); ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); ApexPages.addMessage(msg); acctToByState.ownerId = null; state.state__c = null; lblStateAccounts = false; btnStateAccounts = false; return null; } catch (Exception ex) { ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage()); ApexPages.addMessage(errMsg); return null; } } public void updateAccountOwnership() { for (List<Account> acctsFrom : [Select Id, OwnerId From Account where ownerId =: acctFrom.ownerId]) { for (Account acct: acctsFrom) { acct.OwnerId = acctTo.OwnerId; } update acctsFrom; } } public void updateOpportunityOwnership() { for (List<Opportunity> oppys : [Select Id, OwnerId From Opportunity where ownerId =: acctFrom.ownerId AND stagename <> 'Closed Lost']) { for (Opportunity op: oppys) { op.OwnerId = acctTo.ownerId; } update oppys; } } public void updateCommissionSalesperson() { for (List<Opportunity> oppys : [Select Id, OwnerId From Opportunity where ownerId =: acctFrom.ownerId AND stagename <> 'Closed Lost']) { for (Opportunity op: oppys) { op.Commission_Salesperson__c = acctTo.ownerId; } update oppys; } } public PageReference updateAccountsOpportunities() { try { if (acctChkbox) { updateAccountOwnership(); } if (oppyChkbox) { updateOpportunityOwnership(); } if (oppyEmailChkbox) { updateCommissionSalesperson(); } ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Records updated successfully.'); ApexPages.addMessage(msg); acctFrom.ownerId = null; acctTo.ownerId = null; acctChkbox = false; oppyChkbox = false; oppyEmailChkbox = false; lblAccountsOpportunities = false; btnAccountsOpportunities = false; return null; } catch (Exception ex) { ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage()); ApexPages.addMessage(errMsg); return null; } } public PageReference cancel() { PageReference newpage = new PageReference(System.currentPageReference().getURL()); newpage.setRedirect(true); return newpage; } public Account getAccountFrom() { return acctFrom; } public Account getAccountTo() { return acctTo; } public Boolean acctChkbox { get; set; } public Boolean oppyChkbox { get; set; } public Boolean oppyEmailChkbox { get; set; } public Account getAccountToByParent() { return acctToByParent; } public Account getDistrictAccount() { return parentAcct; } public Account getAccountToByState() { return acctToByState; } public Account getState() { return state; } public Integer ChildAccountCount { get; set; } public Integer StateAccountCount { get; set; } public Integer AccountCount { get; set; } public Integer OpportunityCount { get; set; } public Integer CommissionSalespersonCount { get; set; } public Boolean lblChildAccounts { get; set; } public Boolean lblStateAccounts { get; set; } public Boolean lblAccountsOpportunities { get; set; } public Boolean btnChildAccounts { get; set; } public Boolean btnStateAccounts { get; set; } public Boolean btnAccountsOpportunities { get; set; } static testMethod void test() { Account district = TestUtil.getTestAccount(); DbUtil.save(district); Account account = TestUtil.getTestAccount(); DbUtil.save(account); List<Account> accts = TestUtil.getTestAccounts(5); DbUtil.save(accts); Opportunity opp = TestUtil.getTestOppy(district.Id, district.ownerId); List<Account> children = new List<Account>(); for (Account acct: accts) { acct.parentId = district.Id; children.add(acct); } update children; // Create the controller AccountMaintenanceController Ctrl = new AccountMaintenanceController(); // Set properties Ctrl.acctToByParent.ownerId = district.ownerId; Ctrl.parentAcct.parentId = district.Id; Ctrl.acctToByState.ownerId = district.ownerId; Ctrl.state.state__c = 'TX'; Ctrl.acctFrom.ownerId = district.ownerId; Ctrl.acctTo.ownerId = account.ownerId; Ctrl.acctChkbox = true; Ctrl.oppyChkbox = true; Ctrl.oppyEmailChkbox = true; Ctrl.lblChildAccounts = true; Ctrl.lblStateAccounts = true; Ctrl.lblAccountsOpportunities = true; Ctrl.btnChildAccounts = true; Ctrl.btnStateAccounts = true; Ctrl.btnAccountsOpportunities = true; //Call methods Ctrl.ChildAccountOwnershipCount(); Ctrl.updateChildAccountOwnership(); Ctrl.StateAccountOwnershipCount(); Ctrl.updateAccountByState(); Ctrl.updateAccountOwnership(); Ctrl.updateOpportunityOwnership(); Ctrl.updateCommissionSalesperson(); Ctrl.AccountOpportunityOwnershipCount(); Ctrl.updateAccountsOpportunities(); // Create the controller AccountMaintenanceController Ctrl2 = new AccountMaintenanceController(); // Set properties Ctrl2.acctFrom.ownerId = district.ownerId; Ctrl2.acctTo.ownerId = account.ownerId; Ctrl2.acctChkbox = false; Ctrl2.oppyChkbox = true; Ctrl2.oppyEmailChkbox = true; //Call methods Ctrl2.updateAccountOwnership(); Ctrl2.updateOpportunityOwnership(); Ctrl2.updateCommissionSalesperson(); Ctrl2.AccountOpportunityOwnershipCount(); Ctrl2.updateAccountsOpportunities(); // Create the controller AccountMaintenanceController Ctrl3 = new AccountMaintenanceController(); // Set properties Ctrl3.acctFrom.ownerId = district.ownerId; Ctrl3.acctTo.ownerId = account.ownerId; Ctrl3.acctChkbox = true; Ctrl3.oppyChkbox = false; Ctrl3.oppyEmailChkbox = true; //Call methods Ctrl3.updateAccountOwnership(); Ctrl3.updateOpportunityOwnership(); Ctrl3.updateCommissionSalesperson(); Ctrl3.AccountOpportunityOwnershipCount(); Ctrl3.updateAccountsOpportunities(); // Create the controller AccountMaintenanceController Ctrl4 = new AccountMaintenanceController(); // Set properties Ctrl4.acctFrom.ownerId = district.ownerId; Ctrl4.acctTo.ownerId = account.ownerId; Ctrl4.acctChkbox = true; Ctrl4.oppyChkbox = true; Ctrl4.oppyEmailChkbox = false; //Call methods Ctrl4.updateAccountOwnership(); Ctrl4.updateOpportunityOwnership(); Ctrl4.updateCommissionSalesperson(); Ctrl4.AccountOpportunityOwnershipCount(); Ctrl4.updateAccountsOpportunities(); } }

 

Here are the sections that are not covered:

 

39 0 List<Account> lst = new List<Account>(); 40 0 for (Account acct: childAccts) 41 { 42 0 acct.OwnerId = acctToByParent.OwnerId; 43 0 lst.add(acct); 44 } 45 0 DbUtil.save(lst); 46 47 0 ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); 48 0 ApexPages.addMessage(msg); 49 50 0 acctToByParent.OwnerId = null; 51 0 parentAcct.parentId = null; 52 53 0 lblChildAccounts = false; 54 55 0 btnChildAccounts = false; 56 57 0 return null; ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); 128 0 ApexPages.addMessage(msg); 129 130 0 acctToByState.ownerId = null; 131 0 state.state__c = null; 132 133 0 lblStateAccounts = false; 134 135 0 btnStateAccounts = false; 136 137 0 return null; catch (Exception ex) 216 0 { 217 0 ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage()); 218 0 ApexPages.addMessage(errMsg); 219 0 return null; 220 } 221 } 222 223 0 public PageReference cancel() { 224 0 PageReference newpage = new PageReference(System.currentPageReference().getURL()); 225 0 newpage.setRedirect(true); 226 0 return newpage; 227 }

 

 

Thanks for any help.

SteveBowerSteveBower

Well, conceptually, you have instantiated the controller so now you have access to all the state information.  So, before you call a controller method you can simply remove or modify some of the controllers data such that it throws an exception.

 

So, for example, you have an UpdateAccoutByState() function.  And, you're passing in 'TX' as the state.  You code seems to assume that there will be Accounts from Texas.    (You select all the Texas accounts, then run through a For loop adding records to the Lst, then you update the Lst.)   Well, if there were no Texas records, your select would return nothing, the loop would add nothing to Lst, and then your Update might fail.  At that point the exception would be raised.

 

Or, differently, you could just feed in some garbage data and perhaps that would force it?   What if you set State to 'Peanuts' instead of 'TX'.  What would happen?

 

 

 

However, I think you have an additional conceptual problem.   You're only *exercising* all your code paths.  Nowhere are you actually *verifying* that the code works as it should.

 

For example, if I have code like:

 

 

 

Account acc;   // defined in the Controllers context

 

Function showConcept(Boolean x) {

try { 

if (x) {

acc = new Account(name='ItWasTrue');

         } else {

acc = new Account(name='Darn, False');

}

insert acc;

} catch(Exception e) {

//.... put up an Apex message, etc.

 

 

And I write test code that does:

 

// First try the True case:

// instantiate controller.... etc.

control.showConcept(true);

 

// Now try the False case:

// instantiate controller... etc.

control2.showConcept(false);

 

 

The yes, I've exercised all the code.  100%.  Yeah for me.   However that doesn't mean that it worked.

What if there is a required field to create an Account, and, as you can see, I've left it off.

Each of my test cases would be throwing an exception, however my testing code would never know.

 

So, while you're meeting the Salesforce requirement to cover your code 100%, If I were doing code review I'd reject your code because you haven't actually done something like:

 

// Instantiate controller... etc.

Integer count = [select count() from Account where name = 'ItWasTrue'];  // How many are there when we start

control.showConcept(true); 

Integer newCount =  [select count() from Account where name = 'ItWasTrue'];  // How many now

System.assertNotEquals(null,control.acc.id);   // make sure we got an Id back from the insert.

System.debug('test to see if we've added one and only one ItWasTrue Record'); 

System.assertEquals((count+1), newCount);  // Did we add one.

 

 

 

Hope this helps, Best, Steve.