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
Dave DudekDave Dudek 

how to test a VF page and apex class that only has a SOQL query

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
        }

    }

I've tried writing a test class but i'm getting an unexpected token '=' and i'm also lost at the assertions I need to write.

@isTest
public with sharing class AccountsinPlacesExtTest {

public Place__c placeObj = new Place__c();
public Str placeObj.City__c = 'Boston';
public string placeObj.State_Code__c = 'MA';
//add other relevant attributes
insert placeObj;

//create Account and other records with matching entries
public Account acct = new Account();
string acct.name = 'test1';
string 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();

}
Best Answer chosen by Dave Dudek
Alain CabonAlain Cabon
Hi Dave,

It lacks at least one test Method (two syntaxes) and that causes the first unexpected token '=' ( public Str placeObj.City__c = 'Boston'; )

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm)

@isTest
public with sharing class AccountsinPlacesExtTest {

 static testMethod void myTest() {
    Place__c placeObj = new Place__c();
    placeObj.City__c = 'Boston';
    placeObj.State_Code__c = 'MA';
    insert placeObj;
   ....  
 }

}

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

Regards

All Answers

Alain CabonAlain Cabon
Hi Dave,

It lacks at least one test Method (two syntaxes) and that causes the first unexpected token '=' ( public Str placeObj.City__c = 'Boston'; )

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm)

@isTest
public with sharing class AccountsinPlacesExtTest {

 static testMethod void myTest() {
    Place__c placeObj = new Place__c();
    placeObj.City__c = 'Boston';
    placeObj.State_Code__c = 'MA';
    insert placeObj;
   ....  
 }

}

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

Regards
This was selected as the best answer
David Roberts 4David Roberts 4
and you don't need 'string' in front of the account detail assignment:

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