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
TilluTillu 

Please any one correct this below Test Class ?

I have writtened a test class for below class. But this is controller class. I am unable to cover code for  page reference method and all. please any one improve this code ?


public with sharing class SFDC_Class_Controller_Final{
  
    public list<SFDC_Enrollment__c> Enrolment { get; set; }
    public SFDC_Class__c cls{set;get;}

    public SFDC_Class_Controller_Final(ApexPages.StandardController controller) {
  
        this.cls = (SFDC_Class__c)controller.getRecord();
        if(this.cls.Id != null){
        Enrolment = [select Contact__c, Final_Exam__c, Final_Grade_Value__c, Class__r.Name,Class__r.id from SFDC_Enrollment__c where Class__r.id=:cls.id AND RecordType.Name like '%HLS HFS %'];
     
        }
    }
    public pagereference save(){
        try{
      
        upsert cls;
        upsert Enrolment;
       Pagereference p = new PageReference('/'+cls.Id);
         p.setRedirect(true);
          return p;  
     
        }
        catch(Exception e){}
        return null;
    }
     public pagereference clear(){

     for(SFDC_Enrollment__c enro:Enrolment ){
     enro.Final_Exam__c=null;
     enro.Final_Grade_Value__c=null;
     }
  
     return null;

    } 
}


Here is Test class  :

@isTest
public with sharing class SFDC_Class_Controller_Final_Test{

    static testMethod void  validatestudentdetails(){
    

    SFDC_Enrollment__c sccf=new SFDC_Enrollment__c();
    //sccf.Contact__c='kirthika@gmail.com';
        sccf.Final_Exam__c=100;
        sccf.Final_Grade_Value__c=100;
   insert sccf;
        SFDC_Class__c scc=new SFDC_Class__c();
        scc.name='testclass';
   insert scc;  

}

}
bob_buzzardbob_buzzard
You won't have any coverage from the controller as you don't use it anywhere in your test class.  You need to instantiate the controller using the test record and then execute some methods.  To instantiate the controller, add the following to then end of your test class:

SFDC_Class_Controller_Final controller=new SFDC_Class_Controller_Final(new ApexPages.StandardController(scc));

Then you need to execute some of the methods and confirm that the behaviour is as expected based on your test data.
TilluTillu
Can you please provide some more details if you dont mind