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
Tiffany Shabazz 9Tiffany Shabazz 9 

FormAssembly Test Class for Controller

I am working to integrate FormAssembly forms in Salesforce and need assistance with writing the test class for the code. I would think that this would be something I could easily find since this seems like a common use case, but I am not able to find anything. Has anyone written a test class associated with FormAssembly integration in Salesforce? Essentially, the code is working in the sandbox. I am just trying to display the form using a visualforce page. Here is the code. I used the instructions written by FormAssembly, but those did not include how to create the test class.

Controller:

public class SuggestionBoxController{

    public HTTPResponse res {get; set;}
    public String resBody {get; set;}
    public String endpoint {get; set;}

    public SuggestionBoxController() {
        PageReference pageRef = ApexPages.currentPage();

        HttpRequest req = new HttpRequest();
        req.setMethod('GET');

        if(pageRef.getParameters().get('tfa_next') == null){
          //Replace 'FORM_ID' with your form's ID number
          endpoint = 'https://app.formassembly.com/rest/forms/view/4604281';
        }else{
          endpoint = 'https://app.formassembly.com/rest' + pageRef.getParameters().get('tfa_next');
        }
        req.setEndpoint(endpoint);

        Http http = new Http();
        try {
                //Execute web service call here
                res = http.send(req);
                resBody = res.getBody();
        } catch(System.CalloutException e) {
            //Exception handling goes here....
            System.debug(e);
        }
    }
}

Visualforce Page:

<apex:page controller="SuggestionBoxController">
<apex:outputText value="{!resBody}" escape="false" />
</apex:page>
Ashish KeshariAshish Keshari
Hi Tiffany,
I restructured your code a bit as invoking HTTP Callouts in a constructor is not a good idea.
Please go through the code and the test class achieves 88% for now.
public class SuggestionBoxController{

    public HTTPResponse res {get; private set;}
    public String resBody {get; private set;}
    public String endpoint {get; private set;}

    public SuggestionBoxController() {
        PageReference pageRef = ApexPages.currentPage();
        if(pageRef.getParameters().get('tfa_next') == null){
          //Replace 'FORM_ID' with your form's ID number
          endpoint = 'https://app.formassembly.com/rest/forms/view/4604281';
        }else{
          endpoint = 'https://app.formassembly.com/rest' + pageRef.getParameters().get('tfa_next');
        }
    }
    
    public HttpResponse invokeRESTService(){
        HttpRequest req = new HttpRequest();
        
        req.setMethod('GET');
        req.setEndpoint(endpoint);
        Http http = new Http();
        try {
                //Execute web service call here
                res = http.send(req);
                resBody = res.getBody();
        } catch(System.CalloutException e) {
            //Exception handling goes here....
            System.debug(e);
        }
        return res;

    }
}
 
<apex:page controller="SuggestionBoxController" action="{!invokeRESTService}">
<apex:outputText value="{!resBody}" escape="false" />
</apex:page>
 
@isTest	
public class SuggestionBoxControllerTest {
	
    @isTest static void testSuggestionBox(){
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new SuggestionBoxControllerMock());
        SuggestionBoxController suggestionBox = new SuggestionBoxController();
        HttpResponse result = suggestionBox.invokeRESTService();
        Test.stopTest();
        System.assertEquals(200, result.getStatusCode());
        System.assertEquals('OK', result.getBody());
    }
    
}
 
@isTest
global class SuggestionBoxControllerMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('OK');
        response.setStatusCode(200);
        return response; 
    }
}

 
Tiffany Shabazz 9Tiffany Shabazz 9
Thanks Ashish! Now I am receiving this error when trying to preview the VF Page. Any ideas on what might be causing it? Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexObjectValueELAdapter
Ashish KeshariAshish Keshari
Hi Tiffany,
Sorry there was some error in my code - please use the updated version of these 2 code.

SuggestionBoxController
public class SuggestionBoxController{

    public HTTPResponse res {get; private set;}
    public String resBody {get; private set;}
    public String endpoint {get; private set;}

    public SuggestionBoxController() {
        PageReference pageRef = ApexPages.currentPage();
        if(pageRef.getParameters().get('tfa_next') == null){
          //Replace 'FORM_ID' with your form's ID number
          endpoint = 'https://app.formassembly.com/rest/forms/view/4604281';
        }else{
          endpoint = 'https://app.formassembly.com/rest' + pageRef.getParameters().get('tfa_next');
        }
    }
    
    public PageReference invokeRESTService(){
        HttpRequest req = new HttpRequest();
        
        req.setMethod('GET');
        req.setEndpoint(endpoint);
        Http http = new Http();
        try {
                //Execute web service call here
                res = http.send(req);
                resBody = res.getBody();
        } catch(System.CalloutException e) {
            //Exception handling goes here....
            System.debug(e);
        }
        return null;

    }
}

SuggestionBoxControllerTest
 
@isTest	
public class SuggestionBoxControllerTest {
	
    @isTest static void testSuggestionBox(){
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new SuggestionBoxControllerMock());
        SuggestionBoxController suggestionBox = new SuggestionBoxController();
        suggestionBox.invokeRESTService();
        Test.stopTest();
        System.assertEquals('OK', suggestionBox.resBody);
    }
    
}

 
Ashish KeshariAshish Keshari
Hi Tiffany,
If this helped, please mark the response as best answer. thanks.