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
jaw99KCGjaw99KCG 

Test coverage help please, thanks!

Would love some help getting test coverage for the following:

 

public class accountContactExt {
 
    Account a;

    public accountContactExt(ApexPages.StandardController controller) {
      a = (Account) controller.getRecord();        
    }


    public List<Contact> getSalesContacts() {
      return [select name, title, email, Client_Role__c, phone from contact where accountid = :a.id and Client_Role__c like '%'];
    }
    
  
}

 which feeds a nice inline list to the account page via:

 

<apex:page standardController="Account" extensions="accountContactExt">
 
  <apex:pageBlock title="Client Roles">
    <apex:pageBlockTable value="{!salescontacts}" var="c">
     
      <apex:column headerValue="Contact Name"><apex:outputLink value="{!URLFOR($Action.Contact.View,c.id)}" target="_blank">{!c.name}</apex:outputLink></apex:column>
       <apex:column value="{!c.Client_Role__c}"/>
      <apex:column value="{!c.title}"/>
      <apex:column headervalue="Email"><apex:outputField value="{!c.email}"/></apex:column>
      <apex:column value="{!c.phone}"/>
     
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>
             

 I bet simple for experienced coders but Testing is a real hump for me right now - help getting coverage for this would be appreciated, thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
jaw99KCGjaw99KCG

awesome.

 

Needed to add another acct field to validate in prod, but thanks for the help and patience!

 

 

All Answers

colemabcolemab

You need something like this:

Account TestObjAcct = new Account();
TestObjAcct.Name = 'My test account';
insert TestObjAcct;

ApexPages.StandardController stdController = new ApexPages.StandardController(TestObjAcct); accountContactExt MyContoller = new accountContactExt(stdController); MyController.getSalesContacts();

 Of course, you need to be sure to add data to the required fields for account.

jaw99KCGjaw99KCG

Ah, but do i need to insert contacts, too?

colemabcolemab

Yes, that way you can assert the results from the getSalesContacts function call.

 

Otherwise, that might return null and/or fail.

jaw99KCGjaw99KCG

OK, I want to insert a contact, and do I need to have the account id on the contact look up to the account, too?

 

this is a mess, i can't get past line 4 Error: Compile Error: unexpected token: '=' at line 4 column 17

thanks

public class accountContactExtTest{

Account TestObjAcct = new Account();
TestObjAcct.Name = 'My test account';
insert TestObjAcct;

Contact testClient = new client ();
testClient.Lastname = 'TestClient';

insert testClient;

ApexPages.StandardController stdController = new ApexPages.StandardController(TestObjAcct);

accountContactExt MyContoller = new accountContactExt(stdController);

MyController.getSalesContacts();


}

 

colemabcolemab

This needs to be in a test function, not a class.

 

Here is what the method should look like:

static testMethod void TestaccountContactExt(){

Account TestObjAcct = new Account();
TestObjAcct.Name = 'My test account';
insert TestObjAcct;

Contact testClient = new client ();
testClient.Lastname = 'TestClient';

insert testClient;

ApexPages.StandardController stdController = new ApexPages.StandardController(TestObjAcct);

accountContactExt MyContoller = new accountContactExt(stdController);

MyController.getSalesContacts();


} // end testaccountContactExt

 

jaw99KCGjaw99KCG

OK, so that method won't compile on it's own.

As a static method do I put it in the class? Sorry, thanks

colemabcolemab

This has nothing to do with static, ALL methods must be in a class.  

 

Static just means that it doesn't have polymorphism - the function is the same across all instances of the object.  For that reason, you must call it from the base class and not the instance of the class.

 

Generally, I put the test code for a given class in that class.

jaw99KCGjaw99KCG

Gotcha, sorta. I guess it isn't clear to me then how to fold that test into the class. Or where to put it in and have it like it.

colemabcolemab

It is just another method in the class.  I usually put test methods at the bottom of the class.

 

So for example, your full class (with all methods including test methods) might look like this:

 

public class accountContactExt {
 
    Account a;

    public accountContactExt(ApexPages.StandardController controller) {
      a = (Account) controller.getRecord();        
    } // end accountContactExt


    public List<Contact> getSalesContacts() {
      return [select name, title, email, Client_Role__c, phone from contact where accountid = :a.id and Client_Role__c like '%'];
    } // end getSalesContacts() 

	static testMethod void TestaccountContactExt(){

		Account TestObjAcct = new Account();
		TestObjAcct.Name = 'My test account';
		insert TestObjAcct;

		Contact testClient = new client ();
		testClient.Lastname = 'TestClient';

		insert testClient;

		ApexPages.StandardController stdController = new 			ApexPages.StandardController(TestObjAcct);

		accountContactExt MyContoller = new accountContactExt(stdController);

		MyController.getSalesContacts();


	} // end testaccountContactExt    
  
} // end class accountContactExt 

 

jaw99KCGjaw99KCG

thanks for so much patience. slowly this is coming back to me.

ErrorError: Compile Error: Method does not exist or incorrect signature: MyController.getSalesContacts() at line 29 column 9
 ApexPages.StandardController stdController = new  ApexPages.StandardController(TestObjAcct);

        accountContactExt MyContoller = new accountContactExt(stdController);

        MyController.getSalesContacts();

 So we are not invoking it correctly.  Ah I can I see where it is looking but it looks like to me the method should be there.

colemabcolemab

it is a typo on my part, change this line as pasted:

accountContactExt MyController = new accountContactExt(stdController);

 

Sorry about that :)

colemabcolemab

Please check out my salesforce blog here.

 

If you like it and/or my posts helped you, you can always nominate me for MVP here. :smileyvery-happy:

jaw99KCGjaw99KCG

awesome.

 

Needed to add another acct field to validate in prod, but thanks for the help and patience!

 

 

This was selected as the best answer