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
juniorjjuniorj 

Cannot figure out how to test my controller

Hi there. I have written a controller extension that works a charm.

In one of my methods (that is invoked from the constructor when the page loads),

I do a getDescribe() to get the picklist values for a field. Nothing strange so far... 

 

 

//mandatory controller extension constructor... public StockOrderPageExtension(ApexPages.StandardController stdController) { this.contextStockOrder = (Stock_Order__c) stdController.getRecord(); queryForProductCategories(); showSaveMessage = false; } //This method queries for all the current Bausch product Categories... public void queryForProductCategories() { //Get the Field Token Schema.SObjectField sf = Stock_Product__c.Product_Type__c; //Call describe on the Field Token Schema.DescribeFieldResult sdfr = sf.getDescribe(); //add the defaul sf selectlist value to //the SelectOption list... allProductCategories.add(new SelectOption('null','--None--')); for( Schema.PicklistEntry plentry : sdfr.getPicklistValues()) allProductCategories.add(new SelectOption(plentry.getValue(),plentry.getLabel())); }

Ok. So....

In my test method I have created 1 dummy Stock_Product__c records and given it a Product_Type__c value.

 

 

//Create some Product Categories Stock_Product__c stockProduct = new Stock_Product__c(name='mac', Product_Type__c='Notebook');

 

Fine. So..... 

Now I create an instance of my controller extension.

This invokes the method queryForProductCategories().

I then do a System.debug() on the allProductCategories List in my test method (which is a list of SelectOptions)

 

System.debug('****** ALL PRODUCT CATEGORIES ARE: ' + extensionController.getAllProductCategories());

 But....

I do not see the stock product I created in my test method above.

How on earth can I get around this? I really cannot see a path.

It makes sense in one degree that when teh method queryForProductCategories is invoked (from the test class),

it will get all the picklist values that are committed to the database. 

Has anyone got any help or suggestions? Greatly appreciated.

 

Regards


 

 

 

 

 

DManelskiDManelski
Your method is a void method and therefore isn't returning anything.
KeithJKeithJ

Thanks for the reply.

What are the implications of my method not returning anything? It doesn't need to return anything because I have a method that later on returns a List to the vf page...

 

DManelskiDManelski
Could you post the code for your getter as well?
DManelskiDManelski
Just a quick question as well.  I assume, but I'm just checking, that this picklist you're trying to create by hand is on another object than the sObject of your standard controller?  Could you confirm.
KeithJKeithJ

Yep. The picklist is on another SObject other than that of the standard controller that I am creating my extension for.

 

 

public class StockOrderPageExtension { public Stock_Order__c contextStockOrder; //The current Stock Order referenced from the VF page public String selectedCategory { get; set; } //the currently selected product category public String selectedProduct { get; set; } //Id of the currently selected product public Boolean isProductCategoryNotNone { get; set; } //Tells us if the selected Category is --None-- public Boolean showSKUPageBlockSection { get; set; } public Boolean showSaveMessage { get; set; } public String saveMessageTitle { get; set; } public String saveMessageDetail { get; set; } public List<Order_Line_Detail__c> orderDetailsList; public List<Stock_Product__c> stockProducts = new List<Stock_Product__c>(); public List<SelectOption> allProductCategories = new List<SelectOption>(); //Houses all the product categories public List<SelectOption> allProducts = new List<SelectOption>(); //Houses all the products public List<SKU__c> skuRecordsList = new List<SKU__c>(); //Houses SKU Records for the product in context //mandatory controller extension constructor... public StockOrderPageExtension(ApexPages.StandardController stdController) { this.contextStockOrder = (Stock_Order__c) stdController.getRecord(); queryForProductCategories(); showSaveMessage = false; } //This method queries for all the current Stock product Categories... public void queryForProductCategories() { //Get the Field Token Schema.SObjectField sf = Stock_Product__c.Product_Type__c; //Call describe on the Field Token Schema.DescribeFieldResult sdfr = sf.getDescribe(); //add the defaul sf selectlist value to //the SelectOption list... allProductCategories.add(new SelectOption('null','--None--')); for( Schema.PicklistEntry plentry : sdfr.getPicklistValues()) allProductCategories.add(new SelectOption(plentry.getValue(),plentry.getLabel())); } ... ... /* * Define getters & setters */ public List<SelectOption> getAllProductCategories() { return allProductCategories; } }

 

 

 

KeithJKeithJ
As it stands, I have to hardcode my test values (Which is incorrect, I know) because I can't create a Stock_Product__c in my test code

//Create some Product Categories Stock_Product__c stockProduct = new Stock_Product__c(name='mac', Product_Type__c='Notebook'); //insert these products try { insert stockProduct; }catch(DMLException dmle) { System.debug('***** THERE WAS AN ERROR CREATING TEST PRODUCTS: ' + dmle.getMessage()); }

 This won't work because the actual controller code (Shown in previous post) queries the database and the product I create here is not committed to the database, so i cant use it.

Regards

DManelskiDManelski

I'm not an expert here but I think if you refactored your code to this:

 

 

public List<SelectOption> getProductCategories() { List<SelectOption> options = new List<SelectOption>(); //Get the Field Token Schema.SObjectField sf = Stock_Product__c.Product_Type__c; //Call describe on the Field Token Schema.DescribeFieldResult sdfr = sf.getDescribe(); //add the defaul sf selectlist value to //the SelectOption list... options.add(new SelectOption('null','--None--')); for( Schema.PicklistEntry plentry : sdfr.getPicklistValues()) options.add(new SelectOption(plentry.getValue(),plentry.getLabel())); return options; } public String productCategory { get {return productCategory;} set {productCategory = value;}}

 

 And your VF would look something like this:

 

 

<apex:selectList value="{!ProductCategories}" id="ProductCategories" multiselect="false" size="1"> <apex:selectOptions value="{!productcategory}"/> </apex:selectList>

 

 

 

 

DManelskiDManelski

If I'm reading your post correctly, there may be some confusion about unit testing. SObjects created in tests are committed to the database for the purpose/duration of that test and then backed out at the conclusion. I don't quite understand why you can't create a Stock_Product__c in your test.

 

EDIT: You don't have to hardcode picklist values in your test, there is no reason why you can't perform a describe call in your test too.

Message Edited by DManelski on 02-08-2009 02:11 PM