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
ppiyushppiyush 

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());
    }
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

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

CaptainObviousCaptainObvious

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)...

 

 

Account a = new Account(Name='Test Account');
insert a;

 

 

ppiyushppiyush

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...

 

@isTest
private class childContactTest {

    static testMethod void myUnitTest() {
        Account a = new Account(Name='Test Account');
    	insert a;
    	Contact[] cnx = new Contact[2];
    	cnx[0] = new Contact(FirstName='childc1', LastName='test', account=a);
    	cnx[1] = new Contact(FirstName='childc2', LastName='test', account=a);
    	insert cnx;
    	
		ApexPages.StandardController sc = new ApexPages.StandardController(a);
		childContact cont = new childContact(sc);
		
		system.assertEquals(a,cont.getAt());
		system.assertEquals(cnx,cont.getCntz());
    }
}

 

Here is the actual extension class:

 

public class childContact {
    
    private List<Contact> cntz;
    private Account acct; 

    public childContact(ApexPages.StandardController controller) {
        this.acct= (Account)controller.getRecord();
    }
    
    public Account getAt()
    {
        Account[] at = [Select id,name FROM Account where id= :acct.id];
        if(at.size()>0){
    		return at[0];
    	}
    	else{
    		return null;
    	}
    }
            
    public List<Contact> getCntz()
    {
        Account act = [Select id FROM Account where id = :acct.id];
        cntz = [Select id, Name, Client_Potential__c, title, Address_City__c, Direct_Phone__c, Email, Open_Projects__c, Days_Since_Last_Contact__c, Account.name, Ownerid from Contact where (account.parentid = :act.id OR account.id = :act.id)];
    	return cntz;
    }

    public PageReference UpdateRecords() {
        // this simple line of code finds out which column was changed and update the 
        // relevant account record accordingly!
        update cntz;
        return null;
      }


}

 

JimRaeJimRae

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.

This was selected as the best answer