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
Puja Patil 18Puja Patil 18 

How to write @Test Class

How to write @Test Class for Following Controller :

public with sharing class ReportController { 
                   
    Public Id lookupid{get;set;}
    public Question_Bank_Page__c obj1{get;set;}
    public Question_Option__c lookup;
    public List<Question_Option__c> OptionList{get;set;}
    public List<Question_Option__c> OptionList2{get;set;}
    public Question_Option__c Option{get;set;}
    private ApexPages.StandardController controller;
   
    public PageReference lookupid() {
        
        System.debug('****************Lookup id new*1************************'+obj1.Feedback__c);
        return null;
    }
    public ReportController(ApexPages.StandardController controller) {
        obj1= (Question_Bank_Page__c)controller.getRecord();
         System.debug('****************Lookup id new*2************************'+obj1.Feedback__c); 
    }
    public PageReference PassingParameter(){
        lookupid=obj1.Feedback__c;
        system.debug('++++++++++++++++LOOKUP ID++++++++++++++++++++'+lookupid);
        
        OptionList=[Select Customer_Name__c, CreatedDate, Customer_Email__c, Customer_Phone__c, Response__c, Question__r.Related_Feedback__r.Feedback_Name__c, 
        Question__r.Related_Feedback__r.Count_Question__c, Question__r.Related_Feedback__r.Feedback_Taken_by_Contact__c, Question__r.Question__c, 
        Question__r.Related_Feedback__r.CreatedDate From Question_Option__c where Question__r.Related_Feedback__c=:lookupid Limit 1];
        system.debug('++++++++++++++++++++OPTION LIST++++++++++++++++++++'+OptionList);
        
        OptionList2=[Select Customer_Name__c, CreatedDate, Response__c, Question__r.Question__c From Question_Option__c where Question__r.Related_Feedback__c=:lookupid];
        system.debug('++++++++++++++++++++OPTION LIST++++++++++++++++++++'+OptionList2);
        return null;
    }
Best Answer chosen by Puja Patil 18
Nagendra ChinchinadaNagendra Chinchinada
Hi Puja,
is Feedback__c required field or Masterdeatils relation ship to Question_Bank_Page__c object?.
If there r no required fields and no masterdetail relation ships then below code works. If there are required field/masterdetail relationships then add the same to 'qbp'  object below.
 
public class ReportControllerTest {
static testmethod void testReportController(){
Question_Bank_Page__c qbp = new Question_Bank_Page__c(Name='Test');//Add required fields,related objects like Feedback__c to Question_Bank_Page__c
insert qbp;
Test.StartTest();
ReportController ctrl = new ReportController (new ApexPages.StandardController(qbp));
ctrl.lookupid();
ctrl.PassingParameter();
Test.StopTest();
}
}

Thanks,
Nagendra

All Answers

Nagendra ChinchinadaNagendra Chinchinada
Hi Puja,
is Feedback__c required field or Masterdeatils relation ship to Question_Bank_Page__c object?.
If there r no required fields and no masterdetail relation ships then below code works. If there are required field/masterdetail relationships then add the same to 'qbp'  object below.
 
public class ReportControllerTest {
static testmethod void testReportController(){
Question_Bank_Page__c qbp = new Question_Bank_Page__c(Name='Test');//Add required fields,related objects like Feedback__c to Question_Bank_Page__c
insert qbp;
Test.StartTest();
ReportController ctrl = new ReportController (new ApexPages.StandardController(qbp));
ctrl.lookupid();
ctrl.PassingParameter();
Test.StopTest();
}
}

Thanks,
Nagendra
This was selected as the best answer
Jagadeesh111Jagadeesh111

Hi Puja Patil,

Please find this link for Ref : https://developer.salesforce.com/forums/?id=906F0000000BVY5IAO (https://developer.salesforce.com/forums/?id=906F0000000BVY5IAO" target="_blank)
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

I have provided some idea to do before this create mandatory fields.

@isTest
public class ReportControllerTest
{
    public static testMethod void test1()
    {
        Test.startTest();
            
            // create Parent Obj records here.

            Question_Bank_Page__c objOBPTest = new Question_Bank_Page__c(); // Mention parent id if it has.
            insert objOBPTest;

            Question_Option__c objTest = new Question_Option__c();  // Mention parent id if it has.
            insert objTest; 
            

            ApexPages.StandardController sc = new ApexPages.StandardController(objOBPTest);
            ReportController ctrReport = new ReportController (sc);

            ctrReport.lookupid();
            ctrReport.PassingParameter();
              
    }
}
Please let me know after trying this.

Thanks,
Jagadeesh
Puja Patil 18Puja Patil 18
Thanks Jagadeesh...
I will try it.