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
SakthidasanSakthidasan 

How to write test class for extension controller

I'm new to salesforce any one please explani how to write test class for extension controller class.In the vf page I show the account details and custom object details.If the user not staisfied the result .they create case and task based on the scenerio I create one extension controller.
here is my Controlle Code:
public class MeetingOverviewExtension {
    public final Account accRec;
    public String issueRadio {get; set;}
    transient public Integer counts {get; set;}
    //public List<Meeting__c> meetingList=new List<Meeting__c>();
     public List<Meeting__c> meetingList {get; set; }
   
    public MeetingOverviewExtension(ApexPages.StandardController controller){
        accRec = (Account)controller.getRecord();
        meetingList=[select id,StartDate__c,EndDate__c,Expected_Close_Date__c,Location__c,
                    Purpose__c,OutCome__c from Meeting__c where AccountMaster__c=: accRec.id];
        system.debug('check'+meetingList.size());
        
       
             counts = (Integer) [select count() from meeting__c where AccountMaster__c=: accRec.id and Expected_Close_Date__c = THIS_MONTH];
        System.debug('outttt'+counts);
    }
    public PageReference submit(){
         Task T = new Task();
         Case cRec=new Case();
        if(issueRadio == 'Yes'){
            system.debug('yesstruee');
       
        T.Type = 'Email';
                 T.Description = 'Reassign the outcome'; 
                T.Subject='Outcome monitoring';
    T.WhatId = accRec.Id; 
           
            cRec.Status='New';
            cRec.Status='Email';
            cRec.AccountId=accRec.Id;
     
            }
        try{
            insert T;
            insert cRec;
            system.debug('Caseid::'+cRec.id);
        }
        catch(System.DmlException e){
            System.debug(e.getMessage());
            return null;
                
        }

          PageReference page = new PageReference('/'+accRec.Id);
        page.setRedirect(true);
        return page;

            }
  
}
Test Class:
@isTest 
public class MeetingOverviewExtensionTest {
    
    static testMethod void testMethod1(){
        
        Account acc=new Account(Name='TestMethod');
         insert acc;
         Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
             insert opp;
         Meeting__c mRec=new Meeting__c(AccountMaster__c=acc.id,OutCome_Flag__c=false);
         insert mRec;
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
       // sc.setSelected(acc);
        MeetingOverviewExtension testAccPlan = new MeetingOverviewExtension(sc);
        
         Task T = new Task();
        T.Type = 'Email';
                 T.Description = 'Reassign the outcome'; 
                T.Subject='Outcome monitoring';
                    T.WhatId = acc.Id; 
        insert T;
         PageReference pageRef = Page.MeetingOverviewForm;
       // Test.setCurrentPage(pageRef);
          pageRef.getParameters().put('id', String.valueOf(acc.Id));
        Test.setCurrentPage(pageRef);
    
    }
}
Best Answer chosen by Sakthidasan
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope that will help you
@isTest 
public class MeetingOverviewExtensionTest {
    
    static testMethod void testMethod1()
	{
        
        Account acc=new Account(Name='TestMethod');
        insert acc;
        
		Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
        insert opp;
		
        Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
        insert mRec ;
		
		
		Test.StartTest();
		
			ApexPages.StandardController sc = new ApexPages.StandardController(acc);
			MeetingOverviewExtension testAccPlan = new MeetingOverviewExtension(sc);
			
			PageReference pageRef = Page.MeetingOverviewForm;
			pageRef.getParameters().put('id', String.valueOf(acc.Id));
			Test.setCurrentPage(pageRef);
			testAccPlan.issueRadio='Yes';
			testAccPlan.submit();
			
		Test.StopTest();
		
    }
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope that will help you
@isTest 
public class MeetingOverviewExtensionTest {
    
    static testMethod void testMethod1()
	{
        
        Account acc=new Account(Name='TestMethod');
        insert acc;
        
		Opportunity opp=new Opportunity(Name='TestCaseOpp',StageName='Prospecting',CloseDate=system.today());
        insert opp;
		
        Meeting__c mRec = new Meeting__c( AccountMaster__c = acc.id, OutCome_Flag__c = false );
        insert mRec ;
		
		
		Test.StartTest();
		
			ApexPages.StandardController sc = new ApexPages.StandardController(acc);
			MeetingOverviewExtension testAccPlan = new MeetingOverviewExtension(sc);
			
			PageReference pageRef = Page.MeetingOverviewForm;
			pageRef.getParameters().put('id', String.valueOf(acc.Id));
			Test.setCurrentPage(pageRef);
			testAccPlan.issueRadio='Yes';
			testAccPlan.submit();
			
		Test.StopTest();
		
    }
}

 
This was selected as the best answer
SakthidasanSakthidasan
Is it must to use Test.StartTest()  in that Test class
Amit Chaudhary 8Amit Chaudhary 8
No Test.StartTest() is not required. Please check below post for more information
1) http://amitsalesforce.blogspot.in/2015/02/starttest-and-stoptest-method.html


NOTE :-

1) The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. 
2) All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. 
3) Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.
4) The startTest method does not refresh the context of the test: it adds a context to your test. For example, if your class makes 98 SOQL queries before it calls startTest, and the first significant statement after startTest is a DML statement, the program can now make an additional 100 queries. Once stopTest is called, however, the program goes back into the original context, and can only make 2 additional SOQL queries before reaching the limit of 100
5) All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously