You need to sign in to do that
Don't have an account?

How to test a PageReference Method in an Apex Class
I have a class with two methods. The main method pulls data from the db and creates a list of Opportunities. The PageReference method creates a document using a Page that pulls it's data from the main method. It thens saves this PDF in the Documents folder and as an Attachment on the Opportunity.
I have written test methods for both pieces of code but I'm stuck at 55% coverage. I'm trying to figure out what I'm missing. Thank you in advance.
public class ProbeQuote {
Schema.DescribeFieldResult F = Product2.Family.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();
public Opportunity Probe { get; set; }
public String pdfName;
public ID id = ApexPages.CurrentPage().getParameters().get('id');
public List<Opportunity> ProbeProducts = new List<Opportunity>();
Integer Counter = 1;
public ApexPages.StandardController controller;
public ProbeQuote(ApexPages.StandardController stdController) {
controller = stdController;
//quoteid = stdController.getRecord().id;
//ID id = '0068000000NyTHe';
for (Schema.PicklistEntry fam:P){
Integer i = 0;
String FamilyLabel = fam.GetLabel();
Probe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice,
op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
op.PricebookEntry.Product2.Family, op.LineCount__c
FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
FROM Opportunity o where Id = :id];
Probe.Amount = 0;
Probe.ProductFamily__c = FamilyLabel;
for(i=0;i<Probe.opportunityLineItems.size();i++) {
Probe.Amount += Probe.opportunityLineItems[i].TotalPrice;
Probe.opportunityLineItems[i].LineCount__c = Counter;
Counter++;
}
ProbeProducts.add(Probe);
}
}
public List<Opportunity> getProbeProducts() {
return ProbeProducts;
}
public PageReference attachQuote() {
Opportunity Opp = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c
FROM Opportunity o where Id = :id];
pdfName = Opp.Name + '.pdf';
/* Get the page definition */
PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );
/* set the quote id on the page definition */
pdfPage.getParameters().put('id',id);
/* generate the pdf blob */
Blob pdfBlob = pdfPage.getContent();
/* create the attachment against the quote */
Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);
/* insert the attachment */
insert a;
/* Now create document to show up in Documents Tab in Quotes Folder */
Folder f = [SELECT Id, Name FROM Folder WHERE Name = 'Quotes'
AND Type = 'Document'];
Document d = new Document();
d.Name = pdfName;
//d.DeveloperName = pdfName;
d.ContentType = 'application/pdf';
d.Body = pdfBlob;
d.FolderId = f.Id;
insert d;
/* send the user back to the quote detail page */
return controller.view();
}
public static testMethod void testProbeQuote() {
Id id;
Opportunity testProbe;
List<Opportunity> testProbeProducts = new List<Opportunity>();
Integer Counter = 1;
Schema.DescribeFieldResult F = Product2.Family.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();
Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );
insert testOppty;
id = testOppty.id;
// Insert test Truck OpportunityLineItem
OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');
insert testOLI;
for (Schema.PicklistEntry fam:P){
Integer i = 0;
String FamilyLabel = fam.GetLabel();
testProbe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode, op.PricebookEntry.Product2.Family, op.LineCount__c
FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
FROM Opportunity o where Id = :id];
testProbe.Amount = 0;
testProbe.ProductFamily__c = FamilyLabel;
for(i=0;i<testProbe.opportunityLineItems.size();i++) {
testProbe.Amount += testProbe.opportunityLineItems[i].TotalPrice;
testProbe.opportunityLineItems[i].LineCount__c = Counter;
Counter++;
}
testProbeProducts.add(testProbe);
}
PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);
// Instantiate custom ProbeQuote controller
//testProbeProducts.add(testOppty);
System.assertEquals (testProbeProducts[0].Name, 'Force Monkey 4x4');
ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
ProbeQuote qe = new ProbeQuote(stc);
}
public static testMethod void testattachQuote() {
String pdfName;
Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );
insert testOppty;
ID id = testOppty.id;
// Insert test Truck OpportunityLineItem
OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');
insert testOLI;
pdfName = testOppty.Name + '.pdf';
/* Get the page definition */
PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );
/* set the quote id on the page definition */
pdfPage.getParameters().put('id',id);
/* generate the pdf blob */
Blob pdfBlob = pdfPage.getContent();
/* create the attachment against the quote */
Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);
/* insert the attachment */
insert a;
/* Now create document to show up in Documents Tab in Quotes Folder */
Folder f = new Folder (Name = 'Quotes', Id = '00l80000001DJ2aAAG', Type = 'Document');
System.assertEquals (f.Name, 'Quotes');
Document d = new Document(Name = pdfName, ContentType = 'application/pdf',
Body = pdfBlob, FolderId = f.Id);
insert d;
PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);
// Instantiate custom ProbeQuote controller
ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
ProbeQuote qe = new ProbeQuote(stc);
}
}
You want to look into the standard method called "Test.setCurrentPage()".
public static PageReference redirectToAppropriateAlertsPage_live() { if( alwaysShowTheseInAlertsPageList_live.size()==0) { PageReference acctPage = new PageReference('https://na3.salesforce.com/home/home.jsp'); acctPage.setRedirect(true); return acctPage; } else {PageReference acctPage = new PageReference('https://na3.salesforce.com/apex/alert'); acctPage.setRedirect(true); return acctPage; } return null;}
Test.setCurrentPage( redirectToAppropriateAlertsPage_live() );
System.debug('the current page is...' +ApexPages.currentPage() ); System.assertEquals('/apex/alert', ApexPages.currentPage() );
I've made some updates to the code. I know I have some redunancy but I was just trying everything to make it work. I'm now up to 60% coverage. I don't have any other get/set functions to check that I know of.
Here's the updated test methods. The actual functions and methods can be seen above. I'm hitting the message limit on this post.
I have been unable to install the Force.Com IDE in both Windows and Linux so i have been using the UI Interface in Salesforce. I was able to get a screen grab of what code is not being covered. It's the PageReference AttachQuote function. Everything in my main function is covered.
Thanks for pointing out the feature of seeing which lines of code were covered. Didn't know that was in there.
Any help on tracking this down is appreciated.
I didnt know about %-code , thank you.
I'm running foce.com in eclipse 3.2 - 2.5 on opensuse 11.1 KDE, ubuntu and kubuntu 9.04, but only by just down the archive from eclipse, extract it and just run it from that folder. For all these disto's if i use the package in the repo i get errors. I'm only using 3.4 now for force.com ide, cause i read its the current recommended, and 3.2 is no longer supported.
So I had an epiphany this morning. I had downloaded an app from AppExchange that had the same functionality in it that I was using. I looked at their test method and I figured out the problem.
To test a PageReference method in your code simply call it like the function that it is. I added one line and I now have 100% coverage.
public static testMethod void testProbeQuote() {
/*****
Other code that creates Opportunity and Line Items to test
******/
ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
ProbeQuote qe = new ProbeQuote(stc);
List<Opportunity> testProbeProducts = qe.Getprobeproducts();
System.assertEquals (testProbeProducts[0].Name, 'Force Monkey 4x4');
/***** This is all you need to test a PageReference method/function *****/
qe.attachQuote();
}
I also have the same prob.. Can anybody help me for this...
Do you actually *have* any records in KnowlegeArticleVersion with a UrlName of "12345" with PublishStatus of 'Online'? If not, then you must create some and insert them as part of the test setup so that the query in the init() will discover some records.
Lastly, consider that if you're not checking any of the results of your trigger with system.assert calls, you're not really testing anything. Merely getting code coverage doesn't mean that you've tested anything... just that you've called it.
Best, Steve.