You need to sign in to do that
Don't have an account?
Debra Shoup 4
Test Class for Visualforce Page and Controller
I’m working on my first Visualforce page and controller. Thanks to others on the community, I’ve gotten everything to work and now I need to write the test class to move it to production. I’ve never done this before. I’ve completed the Apex Testing module in Trailhead, but I’m still lost on how to create the test for my project. This just is not clicking for me. Below is my code for the Visualforce page and for the controller. Help is greatly appreciated.
<apex:page StandardController="Contract" extensions="ContractProductsController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!lineItems}" var="oli">
<apex:column >
<apex:facet name="header">Product Code</apex:facet>
{!oli.PricebookEntry.Product2.ProductCode}
</apex:column>
<apex:column >
<apex:facet name="header">Product</apex:facet>
{!oli.PricebookEntry.Name}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ContractProductsController {
public List<OpportunityLineItem> lineItems { get; set; }
Public ContractProductsController( ApexPages.StandardController std )
{
if( std.getRecord().Id != null )
{
Contract con = [ Select Id, Opportunity__c from Contract where Id =: std.getRecord().Id ];
lineItems = [ Select Id, OpportunityID, PricebookEntry.Name, PricebookEntry.Product2.ProductCode,Product2ID,PricebookEntryID
from OpportunityLineItem where OpportunityID =: con.Opportunity__c ];
}
else
{
lineItems = new List<OpportunityLineItem>();
}
}
}
<apex:page StandardController="Contract" extensions="ContractProductsController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!lineItems}" var="oli">
<apex:column >
<apex:facet name="header">Product Code</apex:facet>
{!oli.PricebookEntry.Product2.ProductCode}
</apex:column>
<apex:column >
<apex:facet name="header">Product</apex:facet>
{!oli.PricebookEntry.Name}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ContractProductsController {
public List<OpportunityLineItem> lineItems { get; set; }
Public ContractProductsController( ApexPages.StandardController std )
{
if( std.getRecord().Id != null )
{
Contract con = [ Select Id, Opportunity__c from Contract where Id =: std.getRecord().Id ];
lineItems = [ Select Id, OpportunityID, PricebookEntry.Name, PricebookEntry.Product2.ProductCode,Product2ID,PricebookEntryID
from OpportunityLineItem where OpportunityID =: con.Opportunity__c ];
}
else
{
lineItems = new List<OpportunityLineItem>();
}
}
}
Try the below test class:
Thanks,
Maharajan.C
All Answers
Try the below test class:
Thanks,
Maharajan.C
Thank you! I used your test class and it ran successfully, but I'm only receiving 21% overall code coverage. How do I increase that to at least 75%?
Please share the lines which are not covered. And also check is there any error message.
Thanks,
Maharajan.C
I was receiving an error due to a validation rule on the opportunity. When I deactivated that, there are no more errors, but the overall coverage is still showing as only 22%. I'm not sure where to check to see which lines are not covered. Please let me know what else I can show you.
I've done a test change set from one sandbox to another to see if I run into any deployment issues. I had to deactivate the validation rule in the receiving sandbox envirnonment, but the deployment worked. Thank you SO much!!