+ Start a Discussion
mike83mike83 

Test Classes are killing me.

Ok I have a big nasty controller. At the top I have all these private variables I use Like:

public class pageController {      
private String secondaryLink =''; 

Then I have a get method

 

public String  getRequiredProducts() {
 secondaryLink =''; 
lib = [Select Link__C, Sales_Library_Link_2__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'];
if(lib.Sales__c!=null && lib.Sales_Library_Link_2__c!=null )  { 
 secondaryLink=lib.Sales_Library_Link_2__c;  
}		

 

My problem is I have code coverage on the get require products method but its complaing about not testing the variable secondaryLink... how do I test a private variable in a test class?

 

Heres what I have For the test class

 

 public static testMethod void PageTester()    
{        
string secondaryLink ='';        
string LiveID='00530000001f0fkAAA'; // I know this needs to be a query but dont care right now.       
PageReference pageRef2 = Page.ICHomepage;        
Test.setCurrentPageReference(pageRef2);        
ApexPages.currentPage().getParameters().put('uid', LiveID);        
PageController controller  = new PageController();        
controller.getC();        
controller.getRequiredProducts();         

} 

 So why does it complain about that variable not being covered?  And how do i cover it?

 

Message Edited by mike83 on 07-10-2009 11:26 AM
Message Edited by mike83 on 07-10-2009 11:27 AM
mike83mike83
So heres the deal it says that the if statement is getting 0 executions How do i get those valuse to not be = null.
soofsoof
If I'm understanding this correctly, what you're saying is that the following IF statement does not hold tru, so the code within the IF block does not execute, correct?  

if(lib.Sales__c!=null && lib.Sales_Library_Link_2__c!=null ) { secondaryLink=lib.Sales_Library_Link_2__c; }

 

If that's the issue, then try creating a new Sales_Library__c record in your test code, which meets the query criteria.  Something like the following:

 

Sales_Library__c slib = new Sales_Library__c (Required__c = true, Sales_Library_Category__c = 'Product Information');insert slib;

Place this code right before the controller.getRequiredProducts(); call.  I'm sure you understand that the records created in the test code are NOT committed to the database, so the best practice is to create ALL the data (that your test code needs), right inside your test code.  This way, your test code would not be data dependent and won't start failing if certain data goes missing.

 

Hope this helps!

 

-soof