+ Start a Discussion
IMU AdminIMU Admin 

Test Method for Custom Controller (Apex Class and Visual Force Component)

Hi All,

I need some help writing a test class for the below (I am fairly new to apex so still learning)

Apex Class:

public class InteractionsForCustomer {
    
    
private List<Interactions__c> allInteractions;
        private Interactions__c currentInteractions;
 
    public InteractionsForCustomer() {
         
     }
 
    public List<Interactions__c> getAllInteractions() {
                allInteractions = [Select Id, Name from Interactions__c where Stage__c = 'Open' AND Cutomer__c = :currentInteractions.Customer__c];
        return allInteractions;
    }
 
     public Interactions__c getcurrentInteractions() {       
             return currentInteractions;
     }
     public void setcurrentInteractions(Interactions__c val) {
            this.currentInteractions = val;     
      }
}

VisualForce Component

<apex:component controller="InteractionsForCustomer" access="global">
    <apex:dataTable value="{!allInteractions}" var="inv">
        <apex:column >
            <apex:facet name="header">Invoice Name</apex:facet>
            {!inv.Customer__c}
        </apex:column>
    </apex:dataTable>
</apex:component>

Thanks
Josh
Best Answer chosen by IMU Admin
Tarun_KhandelwalTarun_Khandelwal
Hi Josh,

Please try following class. 
@isTest
private class MyTest {
   static testMethod void InteractionsForCustomerTest(){
		Interactions__c testInterRec = new Interactions__c(Name = 'Test Interactions'; // Also provide any other required field.
		Test.startTest();
		InteractionsForCustomer ifcCtrl = new InteractionsForCustomer ();
		ifcCtrl.setcurrentInteractions(testInterRec);
		List<Interactions__c> interList = ifcCtrl.getAllInteractions();
		Test.stopTest();
	}
}
Also, Please go through the link for learning how to write test class - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

All Answers

KaranrajKaranraj
Try the below test class code
 
​@isTest
public class thecontrollerTests {

    public static testMethod void testMyController() {
       
    //Insert some Customer__c object record
    Customer__c customerObject = new Customer__c();
    customerObject.Name = 'Test Data';
    //Other required fields
    insert customerObject;

   //Insert Interactions__c test data with Stage__c = 'Open' 
    Interactions__c  inter = Interactions__c ();
    inter.Name = 'TestInteraction';
    inter.Stage__c = 'Open';
    inter.Cutomer__c = customerObject.Id;
    insert inter;

    PageReference pageRef = Page.success; //Your pageName
    Test.setCurrentPage(pageRef);  
    InteractionsForCustomer controller = new InteractionsForCustomer ();
    controller = new InteractionsForCustomer(); 
    controller.setcurrentInteractions(inter);
    controller.getcurrentInteractions();
    controller.getAllInteractions();
        System.assertEquals(currentInteractions, inter);
        
    }
}

Check this Trailhead module to learn about writing apex test class in Salesforce https://developer.salesforce.com/trailhead/module/apex_testing
Tarun_KhandelwalTarun_Khandelwal
Hi Josh,

Please try following class. 
@isTest
private class MyTest {
   static testMethod void InteractionsForCustomerTest(){
		Interactions__c testInterRec = new Interactions__c(Name = 'Test Interactions'; // Also provide any other required field.
		Test.startTest();
		InteractionsForCustomer ifcCtrl = new InteractionsForCustomer ();
		ifcCtrl.setcurrentInteractions(testInterRec);
		List<Interactions__c> interList = ifcCtrl.getAllInteractions();
		Test.stopTest();
	}
}
Also, Please go through the link for learning how to write test class - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
This was selected as the best answer
IMU AdminIMU Admin
Hi @Karanraj

I seem to get an error with this code - Line 7 Invalid Type: Customer__c

Customer is the parent object and Interaction is the child object if that makes any difference I am not too sure? 

 
IMU AdminIMU Admin

Hi @Tarun_Khandelwal

I don't get any errors for this but when I check my Apex Class it is still showing as 0% test coverage? 

Sorry if this is simple but haven't written test class before 

Thanks in advance

IMU AdminIMU Admin
Hi @Tarun_Khandelwal

Tell a lie - that has worked 

Thank you very much