You need to sign in to do that
Don't have an account?
Adaniels117
Need Help with Code Coverage for class
Hey SF Forums,
I created a controller that powers a visualforce page and i'm struggling with my test class to get enough code coverage. I've created 2 test classes that only have achieved 16% and 7% coverage and wanted to know if anyone had suggestions as to how i can improve upon it.
Here's the VF Page: "PivotPageCaseOwnerlastweek"
Does anyone have any ideas as to how I can bulk up the coverage....and maybe consolidate the test classes into one? Any advice would be greatly appreciated!
I created a controller that powers a visualforce page and i'm struggling with my test class to get enough code coverage. I've created 2 test classes that only have achieved 16% and 7% coverage and wanted to know if anyone had suggestions as to how i can improve upon it.
Here's the VF Page: "PivotPageCaseOwnerlastweek"
<apex:page controller="pivotsupportcaselastweek" showHeader="FALSE"> <head> <title>Cases Last Week by Owner</title> <apex:stylesheet value="{!$Resource.Pivot}" /> <script type="text/javascript" src="{!$Resource.jquery183min}" ></script> <script type="text/javascript" src="{!$Resource.jqueryui192custommin}" ></script> <script type="text/javascript" src="{!$Resource.PivotJS}" ></script> </head> <style> * {font-family: Verdana;} </style> <script type="text/javascript"> function savedata() { var pivotContent=document.getElementById("output").innerHTML; TestAF(pivotContent); return true; } </script> <apex:form > <div style="overflow: scroll; width: 100%; height: 100%;"> <span class="btn" onclick="return savedata()" > Get Pivot Data</span> <apex:actionfunction action="{!Result}" name="TestAF" rerender="resultPanel" status="TestStatus"> <apex:param assignto="{!pivotContent}" name="FirstParameter" value=""> </apex:param> </apex:actionfunction> <apex:outputpanel id="resultPanel"> <apex:actionstatus id="TestStatus" starttext="Processing..." stoptext=""> <b></b> </apex:actionstatus> <script type="text/javascript"> var InputData={!Data}; $(function(){ $("#output").pivot( InputData , { rows: ["Owner"], cols: ["Day"] } ); }); </script> <div id="output" style="margin: 10px;"></div> </apex:outputpanel> </div> </apex:form> </apex:page>This is the Class:
Public with sharing class pivotsupportcaselastweek { public string pivotContent{get;set;} public string ReturnValue{get;set;} public string getPivot{get;set;} public boolean exportPdf{get;set;} public string getData() { List<PivotData> PivotDataList=new List<PivotData>(); List<Case> CaseList=[Select Account.Name,Owner.Name,Case_Owner_Role__c,Issue__c,Status,Day_Closed__c,Closed_Date__c,ClosedDate,CreatedDate,Type,Reason,Vendor_ID__c,Are_you_a__c, Where__c from case where ClosedDate = LAST_WEEK AND Case_Owner_Role__c='Support Rep' ORDER BY ClosedDate DESC ]; for(Case o :CaseList) { PivotData p=new PivotData(); p.AccountName=o.Account.Name; p.Status=o.Status; p.ClosedDay=o.Closed_Date__c; p.OpenYear=string.valueof(o.CreatedDate.Year()); p.OpenMonth=string.valueof(o.CreatedDate.month()); p.Type=o.Type; p.Issue=o.Issue__c; p.CaseReason=o.Reason; p.VendorID=o.Vendor_ID__c; p.Day=o.Day_Closed__c; p.AreYouA=o.Are_you_a__c; p.Case_Where=o.Where__c; p.ClosedYear=string.valueof(o.ClosedDate.Year()); p.ClosedMonth=string.valueof(o.ClosedDate.month()); p.Owner=o.Owner.Name; p.OwnerRole=o.Case_Owner_Role__c; PivotDataList.add(p); } getPivot='visibility: visible'; exportPdf=false; return JSON.serialize(PivotDataList); } public void Result() { getPivot='visibility: hidden'; exportPdf=true; ReturnValue = 'Save successfully '; } Public PageReference ViewPdf() { PageReference pageRef= new PageReference('/apex/ViewPivot'); pageRef.setredirect(false); return pageRef; } public class PivotData { public string AccountName{get;set;} public string Status{get;set;} public string Owner{get;set;} public string Type{get;set;} public string CaseReason{get;set;} public string AreYouA{get;set;} public string Day{get;set;} public string VendorID{get;set;} public string Issue{get;set;} public string Case_Where{get;set;} public date ClosedDay{get;set;} public string OpenYear{get;set;} public string OpenMonth{get;set;} public string ClosedYear{get;set;} public string ClosedMonth{get;set;} public string OwnerRole{get;set;} } }And my two test classes. This first one achieves 16%:
@isTest public class PivotTestClassv2 { static testMethod void PivotPageCaseOwnerlastweek_Test() { //Test converage for the myPage visualforce page PageReference pageRef = Page.PivotPageCaseOwnerlastweek; Test.setCurrentPageReference(pageRef); Account newAccount = new Account (name='XYZ Organization'); insert newAccount; //create first ccase Case myCase = new Case (Are_you_a__c='Other', Reason='Other', Where__c='Other', Status='Closed', Issue__c='Other', AccountId=newAccount.id); insert myCase; // create an instance of the controller pivotsupportcaselastweek myPageCon = new pivotsupportcaselastweek(); //try calling methods/properties of the controller in all possible scenarios // to get the best coverage. string Type = myPageCon.GetData(); } }And this one gets 7% coverage:
@isTest private class MyPivotTest3 { public static testMethod void pivotsupportcaselastweek() { //Use the PageReference Apex class to instantiate a page PageReference pageRef = Page.PivotPageCaseOwnerlastweek; //In this case, the Visualforce page named 'PivotPageCaseOwnerlastweek;' is the starting point of this test method. Test.setCurrentPage(pageRef); //Instantiate and construct the controller class. pivotsupportcaselastweek controller = new pivotsupportcaselastweek(); //Example of calling an Action method. Same as calling any other Apex method. //Normally this is executed by a user clicking a button or a link from the Visualforce //page, but in the test method, just test the action method the same as any //other method by calling it directly. //The .getURL will return the page url the Save() method returns. String nextPage = controller.ViewPdf().getUrl(); //Check that the save() method returns the proper URL. System.assertEquals('/apex/ViewPivot', nextPage); //Add parameters to page URL ApexPages.currentPage().getParameters().put('qp', 'yyyy'); //Instantiate a new controller with all parameters in the page controller = new pivotsupportcaselastweek(); //Verify that the success page displays System.assertEquals('/apex/ViewPivot', nextPage); } }
Does anyone have any ideas as to how I can bulk up the coverage....and maybe consolidate the test classes into one? Any advice would be greatly appreciated!
I have modified your test class.
Please find the updated code below :
You were not satisfying the condition of Case record in your test class that you have queried in class so i have added the fields in case record that were used in where clause in your query.
Please take care of these things in future.
Let me know if you need more help on this.
Thanks,
Abhishek Bansal.
All Answers
That being said, you're only calling two methods in your class (getData and the default constructor). You're not even testing the Result() or ViewPDF() methods. Once you test those, you'll notice your code coverage will increase a bit.
The next step is to take a closer look at the getData method. The getData method depends on existing Case records in order to function properly. You'll need to create these records in your unit test, as unit tests don't have access to data (other than Users, Communities, and a few other exceptions). You'll notice your code coverage shoot up quite a bit here, because you'll also be creating instances of the PivotDate inner class.
Once you have the code coverage, it's also prudent you start covering edge cases, or cases your users will hit, but that you might not have designed for or handled properly.
The last piece is to handle bulk data insertions, so you'll want to insert several case records to make sure your getData method handles them nicely, etc.
I have modified your test class.
Please find the updated code below :
You were not satisfying the condition of Case record in your test class that you have queried in class so i have added the fields in case record that were used in where clause in your query.
Please take care of these things in future.
Let me know if you need more help on this.
Thanks,
Abhishek Bansal.