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
AbhijitAbhijit 

Need Help to write Test Class

Hi,

 

I am new to apex coding i want to know how i shd write test class for the following code so that it will give max coverage. 

 

 

public class ExtensionOpenActivities{
   
    ApexPages.StandardController stdController;
    public final List<Task> tasks {get; set;}
    public final List<Event> events {get; set;}
    private final Opportunity currentOpportunity;
   
    public String someString {get; set;}       
    public ExtensionOpenActivities(ApexPages.StandardController controller ) {
      
        this.stdController = controller;
        Opportunity currentOpportunity =  (Opportunity)stdController.getRecord();
        this.tasks = new List<Task>();
        this.events = new List<Event>();
                  
             
       
       List<Member_Value_Plan__c> mvpList = [Select id from Member_Value_Plan__c mvp WHERE mvp.Opportunity__r.id = :currentOpportunity.id ];
               
       for(Member_Value_Plan__c mvpObj: mvpList)
        {
           
            List<Task> mvpTasks = [SELECT Id, Subject, ActivityDate, Description, Status, Priority, Type, OwnerId, Owner.Name, CreatedById, CreatedBy.Name FROM Task WHERE WhatID =:mvpObj.id ];
            if(!mvpTasks.isEmpty())
            {
                tasks.addAll(mvpTasks);
            }    
           
            List<Event> mvpEvents = [SELECT Id, EndDateTime, OwnerId, Subject FROM Event WHERE WhatID =:mvpObj.id ];
            if(!mvpEvents.isEmpty())
            {
                events.addAll(mvpEvents);
            }
           
        }
       
       
      }
}

bob_buzzardbob_buzzard

You'll need to create a private class with the @isTest annotation, create at least one method with a signature starting with static testMethod, instantiate your test class and execute the various methods inside it.  You will probably need to set up some test objects to ensure that your soql queries actually return data.

 

There's an example available at: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

prageethprageeth

By writing test methods you can get an Idea about whether your methods give the proper(Expected) output.

Simply the thing you have to is to create an instance of your class and to try to access all the methods in your class one by one and chack whether the output is correct.

You can use System.assertEquals() method to compare results.

You could find a very simple example here. 

 

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=12061#M12061