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
Milan VadhiaMilan Vadhia 

How to write Test Code for given Apex Class?

How can I write Test class for the given Apex class?

Apex Class:
public with sharing class logCallCtrl{
    public Task followUpTask        {   get;set;    } 
    public Boolean isFollowup       {   get;set;    }
    private ApexPages.StandardController stdController;
    public logCallCtrl(ApexPages.StandardController stdController){
        String whoId = ApexPages.currentPage().getParameters().get('who_id');
        String sobjectName = String.isNotBlank(whoId) ? Id.valueOf(whoId).getSObjectType().getDescribe().getName() : '';
        this.stdController = stdController;
        Task objTask = (Task)stdController.getRecord();
        objTask.Status = 'Completed';
        objTask.OwnerID = Userinfo.getUserId();
        objTask.Subject = 'Call';
        objTask.WhatId = ApexPages.currentPage().getParameters().get('what_id');
        objTask.WhoId = sobjectName == 'Lead' || sobjectName == 'Contact' ? objTask.WhoId : null;
        isFollowup = false;  
        followUpTask = new Task(Status = 'Open', OwnerID = Userinfo.getUserId(), Subject = 'Call Follow-up', WhatId=objTask.WhatId, WhoId=objTask.WhoId);
    }
    
    public PageReference save(){  
        String retURL = apexpages.currentpage().getparameters().get('retURL');  
        stdController.save();
        if(isFollowup){
            if(followUpTask.OwnerID == null){
                ApexPages.Message errorMessage1 = new ApexPages.Message(ApexPages.Severity.ERROR,'\'Assigned To\' can not be blank.');
                ApexPages.Message errorMessage2 = new ApexPages.Message(ApexPages.Severity.ERROR,'If you do not want to add Follow-up Task, uncheck \'Add follow-up task?\'');
                ApexPages.addMessage(errorMessage1);
                ApexPages.addMessage(errorMessage2);
                return null;
            } 
            insert followUpTask;
        }
        return String.isNotBlank(retURL) ? new PageReference(retURL) : new PageReference('/'+stdController.getId());
    }
   
}

Visualforce Page:
<apex:page title="Log a Call" standardController="Task" tabStyle="Task" extensions="logCallCtrl">
    <apex:sectionHeader title="Log a Call" subtitle="Log a Call" help="/apex/help page"/>
    <apex:form id="formId">
        <apex:pageMessages />
        <apex:pageBlock id="pageBlockTask" title="Task Edit">
            <div style="border: 0px solid rgb(0,191,255); width:100%;height:23px; background:#CEF6F5;">
                <p style="margin-left:20px;">
                    <table width="100%">
                        <tr><td align="left" width="50%"><B>Task Information </B></td><td align="right" width="30%"><b><span style="color:red;">|</span> = Required Information</b></td></tr>
                    </table>
                </p>
            </div>
            <apex:pageBlockSection id="pbs" columns="2" collapsible="false" >
                <apex:inputField value="{!Task.Subject}" required="true"/>
                <apex:outputLabel />
                <apex:inputField value="{!Task.OwnerId}"/>
                <apex:inputField value="{!Task.ActivityDate}"/>
                <apex:inputField value="{!Task.WhoId}"/>
                <apex:inputField value="{!Task.WhatId}"/>
                <apex:outputField value="{!Task.Status}"/>
                <apex:inputField value="{!Task.Type}" required="true"/>
                <apex:inputField value="{!Task.Description}"/>
            </apex:pageBlockSection>
            <br/>
            <apex:actionRegion >
                <apex:inputCheckbox id="chkbox" value="{!isFollowup}"><b>Add follow-up task?</b>
                     <apex:actionsupport event="onclick" rerender="oppanel"/>
                </apex:inputCheckbox>
                <apex:outputPanel id="oppanel">
                     <apex:outputPanel rendered="{!isFollowup}">
                         <div style="border: 0px solid rgb(0,191,255); width:100%;height:23px; background:#CEF6F5;">
                            <p style="margin-left:20px;">
                                <table width="100%">
                                    <tr>
                                        <td align="left" width="50%"><b>Schedule follow-up task</b></td>
                                        <td align="right" width="30%" style="text-align:right;">
                                        </td>
                                    </tr>
                                </table>
                            </p>
                        </div>
                        <div style="border: 0px solid rgb(0,191,255); width:100%;height:23px; background:#CEF6F5;"><p style="margin-left:20px;">
                            <table width="100%">
                                <tr><td align="left" width="50%"><B>Task Information </B></td><td align="right" width="30%"><b><span style="color:red;">|</span> = Required Information</b></td></tr>
                            </table></p>
                        </div>                
                        <apex:pageBlockSection id="pbs2" columns="2" collapsible="false" >
                            <apex:inputField value="{!followUpTask.Subject}" />
                            <apex:outputLabel />
                            <apex:inputField value="{!followUpTask.OwnerId}" required="false"/>
                            <apex:inputField value="{!followUpTask.ActivityDate}"/>
                            <apex:inputField value="{!followUpTask.WhoId}"/>
                            <apex:inputField value="{!followUpTask.WhatId}"/>
                            <apex:inputField value="{!followUpTask.Status}"/>
                            <apex:inputField value="{!followUpTask.Type}" />
                            <apex:inputField value="{!followUpTask.Description}"/>
                        </apex:pageBlockSection>
                    </apex:outputPanel>
                </apex:outputPanel>
            </apex:actionRegion>
            <apex:pageBlockButtons >
                <apex:commandButton Value="Save" action="{!save}" />
                <apex:commandButton Value="Cancel" action="{!Cancel}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Best Answer chosen by Milan Vadhia
Varun SinghVarun Singh
Hi @sleeping Moneter 

I have  created test class  for  you code ,Its  works  well
@isTest
private class TestingData
{
    static testmethod void myTaskTest()
    {
        test.startTest();
        Task t = new Task(Subject='Donni',Status='New',Priority='Normal',CallType='Outbound');
        ApexPages.StandardController sc = new ApexPages.StandardController(t);    
        logCallCtrl lac= new logCallCtrl(sc);   // Create instance of controller class
        ApexPages.currentPage().getParameters().put('who_id',t.whoid);
        lac.followUpTask=t; 
        lac.isFollowup=true;       
        lac.save(); 
        lac.followUpTask.OwnerID =null;  
        String whoId = ApexPages.currentPage().getParameters().get('who_id');
        test.stopTest();  
              
    }
    
}


If it  is helpful for you ,please make my answer as best answer .

 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sleeping Monster,

Do You got a chance to check the Test generator app from App Exchange please check the below link Please follow below Salesforce Best Practice  Link on Test Classes:-  I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar
 
Varun SinghVarun Singh
Hi @sleeping Moneter 

I have  created test class  for  you code ,Its  works  well
@isTest
private class TestingData
{
    static testmethod void myTaskTest()
    {
        test.startTest();
        Task t = new Task(Subject='Donni',Status='New',Priority='Normal',CallType='Outbound');
        ApexPages.StandardController sc = new ApexPages.StandardController(t);    
        logCallCtrl lac= new logCallCtrl(sc);   // Create instance of controller class
        ApexPages.currentPage().getParameters().put('who_id',t.whoid);
        lac.followUpTask=t; 
        lac.isFollowup=true;       
        lac.save(); 
        lac.followUpTask.OwnerID =null;  
        String whoId = ApexPages.currentPage().getParameters().get('who_id');
        test.stopTest();  
              
    }
    
}


If it  is helpful for you ,please make my answer as best answer .

 
This was selected as the best answer