You need to sign in to do that
Don't have an account?
ppiyush
Testing controller extensions
Hi there,
I have the following controller extension, and corresponding unit test. I want to find out why the assertion fails. How do I initialize the controller in the right way, so that the getAt() function sends back the correct account name?
extension class:
public class childAccount { private List<Account> acctz; private Account acct; public childAccount(ApexPages.StandardController controller) { this.acct= (Account)controller.getRecord(); } public String getAt() { Account[] at = [Select name FROM Account where id= :acct.id]; if(at.size()>0){ return at[0].Name; } else{ return null; } } public List<Account> getAcctz() { Account act = [Select id FROM Account where id = :acct.id]; acctz = [Select id, Name, Address_Street__c, Address_City__c, Office__c, Type, parentid, Ownerid from Account where parentid = :act.id]; return acctz; } }
test class:
@isTest private class childAccountTest { static testMethod void myUnitTest() { Account a = new Account(Name='Test Account'); ApexPages.StandardController sc = new ApexPages.StandardController(a); childAccount cont = new childAccount(sc); system.assertEquals(a.Name,cont.getAt()); } }
You might want to put some logic in your test method to verify that your contacts are actually getting inserted, maybe a try catch block around them. My guess is they are not, I believe you should be adding the accountid, not just account, and referencing the accountID of the test account you insert.
All Answers
Taking a quick glance, I see you created the account object in the test class but did not insert it (therefore it would be null)...
thanks a lot for that - much appreciated.
I have another issue with a similar test that I am running.
Essentially, in the assertion below, I keep getting the value from getCntz() as blank, and hence the assertion fails, but I cant figure out why...
Here is the actual extension class:
You might want to put some logic in your test method to verify that your contacts are actually getting inserted, maybe a try catch block around them. My guess is they are not, I believe you should be adding the accountid, not just account, and referencing the accountID of the test account you insert.