You need to sign in to do that
Don't have an account?

need guidance writing unit test for custom controller
I need some help and guidance writing a unit test for a custom controller I've created.
public class AccountMaintenanceController { private Account acctToByParent = new Account(); private Account parentAcct = new Account(); public AccountMaintenanceController() { } 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; 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 getAccountToByParent() { return acctToByParent; } public Account getDistrictAccount() { return parentAcct; } static testMethod void test() { } }
Sorry for the novice questions, but how do I set the property values in the test method and how do I call the method?
Thanks for any help.
Hi jdl2009,
I am using a customer controller and where I'm having difficulty is understanding how to set my properties in my test method. If I am correct, I need to setup an instance of my constructor to have access to my class in the test method.
In the method in my original post, I have two properties that need to be set with the parentId and with an ownerId, which is what my method is expecting to run the queries. How do I set these in my test method?
Thanks.
Hi jdl2009,
That does make more sense to me. I followed your example and have at least gotten 22% coverage for this class, so I've made some headway.
I did get a test failure on one of my methods: too many query rows Class.AccountMaintenanceController.updateAccountByState
Here is my class file with my test method. Perhaps you can see where I'm making the error?
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); List<Account> accts = TestUtil.getTestAccounts(5); DbUtil.save(accts); List<Account> children = new List<Account>(); for (Account acct: accts) { acct.parentId = district.Id; children.add(acct); } update children; // Create the controller for the event AccountMaintenanceController Ctrl = new AccountMaintenanceController(); //Call methods System.assert(Ctrl.updateChildAccountOwnership() == null); System.assert(Ctrl.updateAccountByState() == null); System.assert(Ctrl.updateAccountsOpportunities() == null); } }
Thanks for the help.
Regards.