1

I'm trying to write a test class to validate a VF page and apex class that only performs a SOQL query to find all accounts matching a city or state. However, I can't figure out what i'm testing for and where i'm testing despite all of the documentation i am reading because it's not just creating a record to test a deletion on but rather just querying a bunch of records.

Here's the original code that you nice folks helped me with but I never put a test class in so it's failing code coverage now...

I have a Place__c record called "Massachusetts" with a State_Code__c field (e.g. "MA"). I'm trying to embed a Visualforce Page on the Place__c Page Layout to show all Account records that have a BillingState that matches the State_Code__c field value of the current record (in this case, "MA"), and have it preferably look like a Related List in LEX.

There is no relationship between place and account. We just want to be able to look at all accounts that have a match to the State Code.


VF Page AccountsinPlaces

<apex:page standardController="Place__c" extensions="AccountsinPlacesExt">    

<apex:stylesheet value="{!URLFOR($Resource.SLDS0122, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />

    <!-- Rest of your page -->



<script> 
function navSObject(reciord) 
{ 
    sforce.one.navigateToSObject(reciord,"detail"); 
};
</script>

<div class="slds" style=".slds-scrollable--x">

<h1>Accounts with Services located in {!Place__c.Name}, and the contacts for those Accounts.</h1>  
<br />NOTE: The specific contacts might not actually be affiliated with the Service shown, this is a list of all contacts associated with the Account.<br />
<apex:dataTable value="{!inPlaceAccounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="slds-table slds-table--bordered">
<apex:Column >
 <apex:facet name="header" >Account Name</apex:facet>
 <apex:outputLink onclick="navSObject('{!Account.Id}')">{!Account.name}</apex:outputLink>
</apex:Column>
<apex:Column >
 <apex:facet name="header">Account JFF Role Name</apex:facet>
<apex:repeat value="{!account.Our_Role_Service_Affiliation_for_Account__r}" var="junction">
<apex:outputLink onclick="navSObject('{!junction.id}')">
       {!junction.Our_Role__r.name}</apex:outputLink><br />
</apex:repeat>
</apex:Column>
<apex:Column >
<apex:facet name="header">Service Name</apex:facet>
<apex:repeat value="{!account.Our_Role_Service_Affiliation_for_Account__r}" var="junction">
   <apex:outputLink onclick="navSObject('{!junction.Service__r.id}')">
       {!junction.Service__r.name}</apex:outputLink><br />
</apex:repeat>
</apex:Column>

<apex:Column >
 <apex:facet name="header">Contact Name</apex:facet>
<apex:repeat value="{!account.Contacts}" var="contact"><apex:outputLink onclick="navSObject('{!contact.id}')">{!contact.name}</apex:outputLink><br /></apex:repeat>
</apex:Column>
</apex:dataTable>
</div>
</apex:page> 

Apex Class AccountsinPlacesExt

public with sharing class AccountsinPlacesExt {
@AuraEnabled

public Place__c place { get; private set; }
public List<Account> inPlaceAccounts { get; private set; }
public AccountsinPlacesExt(ApexPages.StandardController controller)
{
    // add the Place__c field that tells you which state
    List<String> neededFields = new List<String> { 'City__c','State_Code__c' };
    if (!Test.isRunningTest()) controller.addFields(neededFields);
    place = (Place__c)controller.getRecord();

   if (place.City__c != null) {
   // search for city and state match
        inPlaceAccounts = [
            SELECT Id,Name,BillingState,(SELECT Name FROM Contacts),(SELECT Id,Service__r.Name,Our_Role__r.Name FROM Our_Role_Service_Affiliation_for_Account__r) FROM Account WHERE Id IN (SELECT Account__c FROM Our_Role_Service_Affiliation_for_Account__c WHERE Our_Role__r.Name != 'Outreach') AND Our_Role_Count__c > 0 AND BillingCity = :place.City__c AND BillingState = :place.State_Code__c
        ];
   } else {
   //search for only state match
        inPlaceAccounts = [
            SELECT Id,Name,BillingState,(SELECT Name FROM Contacts),(SELECT Id,Service__r.Name,Our_Role__r.Name FROM Our_Role_Service_Affiliation_for_Account__r) FROM Account WHERE Id IN (SELECT Account__c FROM Our_Role_Service_Affiliation_for_Account__c WHERE Our_Role__r.Name != 'Outreach') AND Our_Role_Count__c > 0 AND BillingState = :place.State_Code__c
        ];
   }
   // end else
    }

}
1
  • you have to create test records with matching criteria Commented Apr 14, 2017 at 20:02

2 Answers 2

2

You have to create matching records in the test methods.

Approach will be like this:

//create Place__c record.
Place__c placeObj = new Place__c();
placeObj.City__c = '<enter city name>';
placeObj.State_Code__c = 'MA';
//add other relevant attributes
insert placeObj;

//create Account and other records with matching entries
Account acct = new Account();
acct.name = 'test1';
acct.BillingState = 'MA';
insert acct;

PageReference pageRef = Page.yourVisualforcePage;

Test.startTest();
Test.setCurrentPage(pageRef);

ApexPages.StandardController std = new Apexpages.StandardController(placeObj);    
AccountsinPlacesExt  controller = new AccountsinPlacesExt (std);                    

//assert suitable results.
System.assertEquals(....);
Test.stopTest();
0

You don't have to write a test class for a vf page. What you do have to do is write a test class for the controller, which is the AccountsinPlacesExt class.

What you need to do is create a test class that creates your own test data. Create some dummy accounts with differing BillingStates, then create a Place__c record. After that you can call your controller by writing the following:

Place__c testPlace = [select Id from Place__c];
Test.startTest();
AccountsinPlacesExt controller = new AccountsinPlacesExt(new ApexPages.StandardController(testPlace));
Test.stopTest();

// do some asserts

More information can be found here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .