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
AAlex11AAlex11 

Testing Controller extension

Hi, I've recently written a basic controller extension for a visualforce page, and of course now  I need to test it. 
Problem is I can't reach the necessary code coverage.
I need some help to solve this issue.
That's my controller code :
 
public with sharing class Contact_survey_CX {
     private final Contact ctc;
     private final Contact ctc2;
    
    public Contact_survey_CX(ApexPages.StandardController stdController) {
        this.ctc = (Contact)stdController.getRecord();
        this.ctc2= (Contact)stdController.getRecord(); 
    }
    
    public List<Survey_Question__c> results {
        get {
            if (results == null) {
                results = new List<Survey_Question__c>([SELECT Name, ordernumber__c, question__c
                  FROM Survey_Question__c  WHERE Survey__r.name='Psico-Motivazionale'ORDER BY Survey_Question__c.ordernumber__c ASC]);
                   }           
            return results;
        } 
        private set;
    }
public List<SurveyQuestionResponse__c> results4 {
        get {
           if (results4 == null) {
                results4 = new List<SurveyQuestionResponse__c>([SELECT name,response__c FROM SurveyQuestionResponse__c where Survay__c='Psico-Motivazionale' AND SurveyTaker__r.Contact__r.Id= :ctc2.Id order by Name ,  LastModifiedDate ]);
             }           
            return results4;
        } 
        private set;
        
     }  }
 

 
that's the test class for the controller
@isTest
public class Contact_survey_CXTest{

 static testMethod void testTableSurveyPage() {
 //data for the  test
 Contact ct= new Contact( LastName='test1');
 insert ct;
 Survey__c s2= new Survey__c(name='test1', Submit_response__c='test1');
 insert s2;
 Survey__c s1=[SELECT Id FROM Survey__c  limit 1];
 Survey_Question__c sq1= new Survey_Question__c(Name='question_test_1', survey__c=s1.id, 
 ordernumber__c=4000, type__c='nessuno', question__c='text_quest_1');
 insert sq1;
 
 Survey_Question__c sq2=[SELECT Id FROM Survey_Question__c  limit 1];
SurveyTaker__c st2= new SurveyTaker__c(taken__c='text1');
insert st2;
 SurveyTaker__c st1= [SELECT Id FROM SurveyTaker__c   limit 1];
 
 SurveyQuestionResponse__c sr1= new SurveyQuestionResponse__c(Survey_Question__c= sq2.id, SurveyTaker__c=st1.id);
 insert sr1;
 
 // create a new Contact standard controller by passing it the account record
    ApexPages.StandardController controller = new ApexPages.StandardController(ct);

    // now pass it to the extension
   Contact_survey_CX stdController = new Contact_survey_CX(controller);
 ApexPages.currentPage().getParameters().put('id',st1.id );
    system.assert(stdController != null); // controller has successfully been created
   

        
 }
}
This test class pass but unfortunately works just for the first 7 rows.
It would be great if someone could help me write the correct test class for this controller.

 
Best Answer chosen by AAlex11
Nayana KNayana K

Just want to know : 

this.ctc = (Contact)stdController.getRecord();
this.ctc2= (Contact)stdController.getRecord();

Any specific reason for using 2 varibales ctc, ctc2 to hold same value? By looking into code it seems only one variable is enough.
 
@isTest
public class Contact_survey_CXTest
{

 static testMethod void testTableSurveyPage() 
 {
	 //data for the  test
	 Contact objCon = new Contact( LastName='test1');
	 insert objCon;
	 
	 Survey__c objSurvey = new Survey__c(name='Psico-Motivazionale', Submit_response__c='test1');
	 insert objSurvey;
	 
	 Survey_Question__c objSQ = new Survey_Question__c(Name='question_test_1', survey__c=objSurvey.id, 
	 ordernumber__c=4000, type__c='nessuno', question__c='text_quest_1');
	 insert objSQ;
	 
	
	 SurveyTaker__c objST = new SurveyTaker__c(taken__c='text1', Contact__c = objCon.id);
	 insert objST;
      
	 
	 SurveyQuestionResponse__c objSR = new SurveyQuestionResponse__c(Survey_Question__c= objSQ.id, SurveyTaker__c = objST.id,
																	Survay__c='Psico-Motivazionale');
	 insert objSR;
 
    // replace YOURPAGENAME with actual page name
    PageReference pageRef = Page.YOURPAGENAME;
    
    Test.setCurrentPage(pageRef);
		
	// create a new Contact standard controller by passing it the account record
    ApexPages.StandardController sc = new ApexPages.StandardController(objCon);
	
    // now pass it to the extension
    Contact_survey_CX stdController = new Contact_survey_CX(sc);
	
	// verify if one row returned
    system.assertEquals(1, stdController.results.size() > 0);
	
	// verify if one row returned
    system.assertEquals(1, stdController.results4.size() > 0);
 }
}

 

All Answers

Nayana KNayana K

Just want to know : 

this.ctc = (Contact)stdController.getRecord();
this.ctc2= (Contact)stdController.getRecord();

Any specific reason for using 2 varibales ctc, ctc2 to hold same value? By looking into code it seems only one variable is enough.
 
@isTest
public class Contact_survey_CXTest
{

 static testMethod void testTableSurveyPage() 
 {
	 //data for the  test
	 Contact objCon = new Contact( LastName='test1');
	 insert objCon;
	 
	 Survey__c objSurvey = new Survey__c(name='Psico-Motivazionale', Submit_response__c='test1');
	 insert objSurvey;
	 
	 Survey_Question__c objSQ = new Survey_Question__c(Name='question_test_1', survey__c=objSurvey.id, 
	 ordernumber__c=4000, type__c='nessuno', question__c='text_quest_1');
	 insert objSQ;
	 
	
	 SurveyTaker__c objST = new SurveyTaker__c(taken__c='text1', Contact__c = objCon.id);
	 insert objST;
      
	 
	 SurveyQuestionResponse__c objSR = new SurveyQuestionResponse__c(Survey_Question__c= objSQ.id, SurveyTaker__c = objST.id,
																	Survay__c='Psico-Motivazionale');
	 insert objSR;
 
    // replace YOURPAGENAME with actual page name
    PageReference pageRef = Page.YOURPAGENAME;
    
    Test.setCurrentPage(pageRef);
		
	// create a new Contact standard controller by passing it the account record
    ApexPages.StandardController sc = new ApexPages.StandardController(objCon);
	
    // now pass it to the extension
    Contact_survey_CX stdController = new Contact_survey_CX(sc);
	
	// verify if one row returned
    system.assertEquals(1, stdController.results.size() > 0);
	
	// verify if one row returned
    system.assertEquals(1, stdController.results4.size() > 0);
 }
}

 
This was selected as the best answer
AAlex11AAlex11
Thanks a lot Nayana K!
You're right I can use just one variable ctc,I rectify my code just now.
But I 've this problem now:
the system.assertEquals give me this error :
Comparison arguments must be compatible types: Integer, Boolean in row 39 of your code, How Can I fix this??
Any suggestions?
 
Swaraj Behera 7Swaraj Behera 7
Use below code.
@isTest
public class Contact_survey_CXTest
{

 static testMethod void testTableSurveyPage() 
 {
   //data for the  test
   Contact objCon = new Contact( LastName='test1');
   insert objCon;
   
   Survey__c objSurvey = new Survey__c(name='Psico-Motivazionale', Submit_response__c='test1');
   insert objSurvey;
   
   Survey_Question__c objSQ = new Survey_Question__c(Name='question_test_1', survey__c=objSurvey.id, 
   ordernumber__c=4000, type__c='nessuno', question__c='text_quest_1');
   insert objSQ;
   
  
   SurveyTaker__c objST = new SurveyTaker__c(taken__c='text1', Contact__c = objCon.id);
   insert objST;
      
   
   SurveyQuestionResponse__c objSR = new SurveyQuestionResponse__c(Survey_Question__c= objSQ.id, SurveyTaker__c = objST.id,
                                  Survay__c='Psico-Motivazionale');
   insert objSR;
 
    // replace YOURPAGENAME with actual page name
    PageReference pageRef = Page.YOURPAGENAME;
    
    Test.setCurrentPage(pageRef);
    
  // create a new Contact standard controller by passing it the account record
    ApexPages.StandardController sc = new ApexPages.StandardController(objCon);
  
    // now pass it to the extension
    Contact_survey_CX stdController = new Contact_survey_CX(sc);
  
  // verify if one row returned
    system.assertEquals(1, stdController.results.size() );
  
 
 }
}

Thanks
AAlex11AAlex11
I've just figured out! I've solved the problem and I've achieved 100% of test coverage.
Thanks anyway guys!
Nayana KNayana K
// I am extremely sorry. Silly me
system.assertEquals(1, stdController.results.size());
// verify if one row returned
 system.assertEquals(1, stdController.results4.size() );
Nayana KNayana K
Please mark this as solved