Get Pivot Data
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 PivotDataList=new List(); List 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!", "answerCount": 5, "upvoteCount": 0, "datePublished": "2015-12-22T14:42:26.000Z", "author": { "@type": "Person", "name": "Alex Daniels", "url": "https://trailblazers.salesforce.com/profileView?u=00530000007rASVAA2", "affiliation": { "@type": "Organization", "name": "WeddingWire" } }, "acceptedAnswer": { "@type": "Answer", "text": "Hi, I have modified your test class. Please find the updated code below : @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', ClosedDate = toStartOfWeek().addDays(-4), Case_Owner_Role__c = 'Support Rep', 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. myPageCon.GetData(); myPageCon.result(); myPageCon.viewPDF(); } } 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.", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4CuySAF", "datePublished": "2015-12-22T16:55:00.000Z", "author": { "@type": "Person", "name": "Abhishek Bansal", "url": "https://trailblazers.salesforce.com/profileView?u=0054S000002zQLfQAM", "affiliation": { "@type": "Organization", "name": "Techemega Technology opc Pvt Ltd" } } }, "suggestedAnswer": [ { "@type": "Answer", "text": "Were you able to increase the coverage? i am stuck at 22%... The code related to PivotData is not covered...", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4CuySAF", "datePublished": "2016-05-03T20:38:31.000Z", "author": { "@type": "Person", "name": "Victor Cazacu", "url": "https://trailblazers.salesforce.com/profileView?u=0053000000BiJmZAAV", "affiliation": { "@type": "Organization", "name": "D+H" } } } ] } } Skip to main content
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!

 
5 个回答
  1. 2015年12月22日 16:55
    Hi,

    I have modified your test class.

    Please find the updated code below :

    @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',

    ClosedDate = toStartOfWeek().addDays(-4),

    Case_Owner_Role__c = 'Support Rep',

    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.

    myPageCon.GetData();

    myPageCon.result();

    myPageCon.viewPDF();

    }

    }

    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.
0/9000