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
Debra Shoup 4Debra 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>();
                                }
    }        
}
Best Answer chosen by Debra Shoup 4
Maharajan CMaharajan C
Hi Debra,

Try the below test class:
 
@isTest
public class ContractProductsControllerTest {
    
    Public static testmethod void testContractProduct(){
        
        Account acc = new Account();
        acc.Name = 'test account';
        insert acc;
        
        Id pbID = test.getStandardPricebookId();
        Opportunity o = new Opportunity();
        OpportunityLineItem ol = new OpportunityLineItem();
        
        Product2 product1 = new Product2(Name = 'Product1', 
                                         IsActive = true);
        insert product1;
        
        PricebookEntry PricebookEntry1 = new PricebookEntry(Product2Id = product1.ID,
                                                             Pricebook2Id = pbID,
                                                             UnitPrice = 1,
                                                             IsActive = true);
        insert PricebookEntry1;
        
        o.AccountId = acc.Id;
        o.Name = 'Test_Joe_123';
        o.StageName = 'Prospecting';
        o.CloseDate = date.today();
        o.Type = 'New Business';
        insert o;
        
        ol.OpportunityId = o.Id;
        ol.Quantity = 1;
        ol.UnitPrice = 2.00;
        ol.PricebookEntryId = PricebookEntry1.Id;
        ol.Product2Id = product1.Id;
        insert ol;
        
        Contract C = New Contract();
        C.AccountId = acc.Id;
        C.Opportunity__c = o.Id;
        C.Status = 'Draft';
        C.StartDate = Date.today();
        C.ContractTerm = 2;
        Insert C;

        ContractProductsController obj = new ContractProductsController( new ApexPages.StandardController( C ) );  
        
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Debra,

Try the below test class:
 
@isTest
public class ContractProductsControllerTest {
    
    Public static testmethod void testContractProduct(){
        
        Account acc = new Account();
        acc.Name = 'test account';
        insert acc;
        
        Id pbID = test.getStandardPricebookId();
        Opportunity o = new Opportunity();
        OpportunityLineItem ol = new OpportunityLineItem();
        
        Product2 product1 = new Product2(Name = 'Product1', 
                                         IsActive = true);
        insert product1;
        
        PricebookEntry PricebookEntry1 = new PricebookEntry(Product2Id = product1.ID,
                                                             Pricebook2Id = pbID,
                                                             UnitPrice = 1,
                                                             IsActive = true);
        insert PricebookEntry1;
        
        o.AccountId = acc.Id;
        o.Name = 'Test_Joe_123';
        o.StageName = 'Prospecting';
        o.CloseDate = date.today();
        o.Type = 'New Business';
        insert o;
        
        ol.OpportunityId = o.Id;
        ol.Quantity = 1;
        ol.UnitPrice = 2.00;
        ol.PricebookEntryId = PricebookEntry1.Id;
        ol.Product2Id = product1.Id;
        insert ol;
        
        Contract C = New Contract();
        C.AccountId = acc.Id;
        C.Opportunity__c = o.Id;
        C.Status = 'Draft';
        C.StartDate = Date.today();
        C.ContractTerm = 2;
        Insert C;

        ContractProductsController obj = new ContractProductsController( new ApexPages.StandardController( C ) );  
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Debra Shoup 4Debra Shoup 4
Hi Maharajan,

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%?
Maharajan CMaharajan C
I am getting 85 percentage.

User-added image

Please share the lines which are not covered. And also check is there any error message.

Thanks,
Maharajan.C
Debra Shoup 4Debra Shoup 4
Maharajan, 

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. User-added image

 
Debra Shoup 4Debra Shoup 4
Wait - I'm an idiot.  I see it now. I have 85% code coverage after deactivating the validation rule.

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!!

User-added image