• Victor Cazacu
  • NEWBIE
  • 0 Points
  • Member since 2016


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
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"
<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 am trying to figure out how to translate a list of Close Dates in the respective Fiscal Quarters.  We have custom fiscal years enabled in the org.

The code below is completely functional, except line 13.  How do convert the Close Dates to Fiscal Quarters.

Thanks!
 
public string getData()
  {    List<PivotData> PivotDataList=new List<PivotData>();
   List<OpportunityLineItem> OppList=[Select Opportunity.Account.Name,Opportunity.StageName,Opportunity.CloseDate,
   Opportunity.Amount,Opportunity.RecordTypeID,CurrencyISOCode,Expected_Total_Price__c,Opportunity.IsClosed, Quantity,
   Opportunity.Name,PriceBookEntry.Product2.Name,Opportunity.Probability,PriceBookEntry.Product2.Description from OpportunityLineItem 
   WHERE Opportunity.OwnerID =: UserInfo.getUserId() AND Opportunity.IsClosed = FALSE ];
       for(OpportunityLineItem o :OppList)
       {
           PivotData p=new PivotData();
           p.AccountName=o.Opportunity.Account.Name;
           p.StageName=o.Opportunity.StageName;
           p.Year=string.valueof(o.Opportunity.CloseDate.Year());
           p.Quarter=string.valueof(o.Opportunity.CloseDate.FiscalQuarter());
           p.CurrencyCode=o.Opportunity.CurrencyISOCode;
           p.ExpectedTotalPrice=o.Expected_Total_Price__c;
           p.OpportunityName=o.Opportunity.Name;
           p.ItemNumber=o.PriceBookEntry.Product2.Name;
           p.Probability=o.Opportunity.Probability;
           p.ItemDescription=o.PriceBookEntry.Product2.Description;
           p.Quantity=o.Quantity;
           
           PivotDataList.add(p);
           
       }
       getPivot='visibility: visible';
       exportPdf=false;
       return JSON.serialize(PivotDataList);
  }