function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mike83mike83 

Apex Test Classes Question

Ok So i have this big nasty controller and I am hitting 70% on my code coverage. There are no setters only getters. Which I have to do because sites doesnt let you use a standard controller. heres one of the pieces its complaining about.

 

public class repPageController { 

 String prods='';

public String getRequiredProducts()

{prods= '';

lib = [Select Link__C, Sales__c, Thumbnail__c, Headline__c, Teaser_Text__c From Sales_Library__c

Where Required__c = true AND Sales_Library_Category__c = 'Product Information'];

prods='<div class="floatLeft" style="width:70%"><p><h3><a href="'+lib.Link__c +'" target="_blank">'

+lib.Headline__c+'</a></h3><br/>'+lib.Teaser_Text__c+'&nbsp;&nbsp;<a href="'+lib.Link__c

+'" target="_blank">More...</a>'+secondaryLink+' </p></div><div class="floatRight" style="width:28%">'

+imgLink+'</div><div class"clear"></div>';

return prods; 

} 

 

 

So heres my test statement.

 

public class TestRepPageControllers {

  public static testMethod void RepPageTester()

{

  string LiveID='00530000001f0fkAAA';

PageReference pageRef2 = Page.ICHomepage;

Test.setCurrentPageReference(pageRef2);

ApexPages.currentPage().getParameters().put('uid', LiveID);

RepPageController controller = new RepPageController();

controller.getRequiredProducts();

 

} 

 

Best I can tell its saying that I dont have coverage on the Lib variable But I am not sure how else to any help would be great

 

Message Edited by mike83 on 05-11-2009 03:14 PM
Best Answer chosen by Admin (Salesforce Developers) 
thecrmninjathecrmninja

Mike,

 

To my eye, it seems you are not getting coverage on the Library line because you're not 'hitting' it as part of your test class.  

It would seem to me that you need to construct a Libraby record in your test method to get called as part of the controller method.  Them, use systemassert to make sure your URL actually contains the Link_C value you specified in your test object.  

All Answers

thecrmninjathecrmninja

Mike,

 

To my eye, it seems you are not getting coverage on the Library line because you're not 'hitting' it as part of your test class.  

It would seem to me that you need to construct a Libraby record in your test method to get called as part of the controller method.  Them, use systemassert to make sure your URL actually contains the Link_C value you specified in your test object.  

This was selected as the best answer
mike83mike83
So when I return the 'prods' variable which includes in it the library line it doesn't execute and test that? The deeper I get into this thing the more I realize that customization is not a quick thing like they sell.
Message Edited by mike83 on 05-12-2009 08:40 AM
thecrmninjathecrmninja

Customization is really two things, isn't it?  One is configuration, which is very easy in Salesforce.  The other is programming which is more challenging but, the more I've been exposed to SF's programming structure, the more I've found it to be easy.  The big challenge is learning what it expects and, after that, you get pretty home free. 

 

Personally, I really try to control every aspect of the process in my test as possible.  For instance, I see no system assertion in your method which means the Test is only running your controller, not making sure that it's achieving the desired results.  

Constructing all of your objects within the Test Method will allow you to do that, across Orgs (this is the most crucial part).  Right now, if you try to deploy that method to Production and you don't have a Library record with "Product Information", your method will fail.  However, if you create a library object within the method, then the method references itself to test your controller and you're not relying on your ORG db, which is not so tightly controlled.

It's not 'easy' but, I would suggest that it's every bit of necessary.  In constructing test methods, I've had many unforeseen issues/inefficiencies become apparent for code that was functional.  If not for that, I would have deployed code and discovered its problems after my users had come to depend on it.  

This is just my take Mike and I am by no means an expert or trying to push my opinion on you.  Hopefully, you find SF programming to be as complementary a tool as I have.  

If you do create a test method that ups your coverage, please be sure to share it here.