Controller class
public class TastingProductClass {
public Tasting_Product__c prod {get; set; }
String prodId;
String tasteId;
String retUrl;
public TastingProductClass(ApexPages.StandardController stdcon) {
prodId = ApexPages.currentPage().getParameters().get('id');
tasteId = ApexPages.currentPage().getParameters().get('CF00N50000003LHz0_lkid');
retUrl = ApexPages.currentPage().getParameters().get('retUrl');
if(prodId!= NULL && prodId !='') {
this.prod = (Tasting_Product__c)stdcon.getRecord();
} else {
prod = new Tasting_Product__c();
prod.Tasting__c = tasteId;
}
}
public PageReference Save() {
upsert(prod);
Tasting__c taste = new Tasting__c(id=tasteId);
PageReference page = new ApexPages.StandardController(taste).View();
return page;
// return null;
}
public PageReference Cancel() {
return null;
}
public PageReference SaveNew() {
upsert(prod);
prod = new Tasting_Product__c();
prod.Tasting__C = tasteId;
PageReference page = new ApexPages.StandardController(prod).view();
return page;
}
}
Testing Class
@isTest
public class TastingProductTest {
static list<Client_Plan__c> client;
@isTest static public void TestTastingProduct() {
Account acct = new Account(name = 'test');
Tasting__c taste = new Tasting__c(Name = 'test', Account__c = acct.id);
Tasting_Product__c prod = new Tasting_Product__c (Tasting__c = taste.id);
test.startTest();
//Instantiate and construct the controller class.
ApexPages.currentPage().getParameters().put('id', prod.id);
ApexPages.StandardController myPlan = new ApexPages.StandardController(prod);
TastingProductClass controller = new TastingProductClass(myPlan);
controller.save(); //getting an error here
controller.saveNew();
controller.Cancel();
test.stopTest();
}
}
The actual visualforce page
Also, what if I want to test the new page funtionality, how would I vary the test class?Thanks,<apex:page standardController="Tasting_Product__c" extensions="TastingProductClass" >
<apex:pageblock >
<apex:form >
<apex:commandButton value="Save" Action="{!save}"/>
<apex:commandButton value="Cancel" Action="{!cancel}"/>
<apex:pageblocktable value="{!prod}" var="pd">
<apex:column >
<apex:facet name="header">Brand</apex:facet>
<apex:inputfield value="{!pd.Brand__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Tier</apex:facet>
<apex:inputfield value="{!pd.Tier__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Variety</apex:facet>
<apex:inputfield value="{!pd.Variety__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Retail Price</apex:facet>
<apex:inputfield value="{!pd.Retail_Price__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Samples Given</apex:facet>
<apex:inputfield value="{!pd.Samples_Given__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Bottles Sold</apex:facet>
<apex:inputfield value="{!pd.Bottles_Sold__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Product Placement</apex:facet>
<apex:inputfield value="{!pd.Product_Placement__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">Consumer Comments</apex:facet>
<apex:inputfield value="{!pd.Consumer_Comments__c}" />
</apex:column>
</apex:pageblocktable>
</apex:form>
</apex:pageblock>
</apex:page>