You need to sign in to do that
Don't have an account?
Milan Vadhia
How to write Test Code for given Apex Class?
How can I write Test class for the given Apex class?
Apex Class:
Visualforce Page:
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>
I have created test class for you code ,Its works well
If it is helpful for you ,please make my answer as best answer .
All Answers
Do You got a chance to check the Test generator app from App Exchange please check the below link
- https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD (https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD )
Please follow below Salesforce Best Practice Link on Test Classes:-- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
- https:// developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm (https:// developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm )
- http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
I hope it will be helpful.Please mark it as best answer if the information is informative.
Best Regards
Rahul Kumar
I have created test class for you code ,Its works well
If it is helpful for you ,please make my answer as best answer .