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
Upton_XUpton_X 

Test Class for VFP Controller

My organization uses Conga to manage contracts. These contracts can have up to 5 related “Legal Entities”, and we have five Legal Entity lookup fields on the Contract Agreement object. Ideally we would use a junction object, but I was overruled. Now I am trying to write APEX that combines all related agreements on the Legal Entity record page, regardless which legal entity field they are related to, and display it on a visual force page.


I’ve stumbled through writing the controller and visualforce page.

However, I am having trouble with achieving 75% code coverage with a test .


Here is my class
public class CombineContractAgreement {
    Id LegalEntityId;
public CombineContractAgreement(ApexPages.StandardController controller){
    LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id;
    }

    public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement()
    {
     List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements;
        AllRelatedAgreements = [SELECT Name, Agreement_Type__C, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c,
        Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR 
                                Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId];
                                    return AllRelatedAgreements;
    }
}

Visualforce
<apex:page StandardController="SB_Legal_Entity__c" extensions="CombineContractAgreement" lightningStylesheets="true">
    <apex:pageBlock title="All Related Agreements">
        <apex:pageBlockTable value="{!AllRelatedAgreement}" var="item">
            <apex:column >
                <apex:outputLink value="https://hyclonehorizon--sb1.lightning.force.com/{!item.ID}">{!item.Name}</apex:outputLink>
            </apex:column>    
            <apex:column value="{!item.Agreement_Name__c}" />
            <apex:column value="{!item.Agreement_Type__c}" />
            <apex:column value="{!item.APXT_Redlining__Status__c}" />
            <apex:column value="{!item.APXT_Redlining__Effective_Date__c}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Test Class
@isTest
public class CombineContractAgreementTest {
    
    static testMethod void testMethod1(){

        //Create required legal alias for Legal Entitiy Object
        Legal_Alias__c ali = new Legal_Alias__c();
        ali.Name     = 'NameLegalAlias';
        system.debug('The Legal Alias name is '+ali.Name);
        insert ali;
        

		//Create Legal Entity
        SB_Legal_Entity__c testEntity = new SB_Legal_Entity__c();
        testEntity.Legal_Alias__c = ali.Id;
        testEntity.Name     = 'testLegalEntity';
        insert testEntity;
        
        //Create Contract Agreement
        APXT_Redlining__Contract_Agreement__c testAgreement = new APXT_Redlining__Contract_Agreement__c();
        testAgreement.Customer_Legal_Entity__c          = testEntity.id;
        testAgreement.APXT_Redlining__Status__c         = 'inactive';
        testAgreement.APXT_Redlining__Effective_Date__c = date.today();
        testagreement.Agreement_Name__c                 = 'TestAgreement';
        insert testAgreement;
        System.debug('The Contract Agreements Legal Entity is' + testAgreement.Customer_Legal_Entity__c);
        
        
      Test.StartTest();
        //current page equal the legal entity page
        ApexPages.StandardController stdctrl = new ApexPages.StandardController(testEntity);
        ApexPages.currentPage().getParameters().put('id',testEntity.Id);
        CombineContractAgreement testCombineAgree = new CombineContractAgreement(stdctrl);
        stdctrl.cancel();
      Test.Stoptest();

        }


        
    }

Result:
User-added image​​​​​​​
 
Best Answer chosen by Upton_X
Andrew GAndrew G
invoke the method against the page
Test.StartTest();
        //current page equal the legal entity page
        ApexPages.StandardController stdctrl = new ApexPages.StandardController( testEntity );
        ApexPages.currentPage().getParameters().put('id',testEntity.Id);
        CombineContractAgreement testCombineAgree = new CombineContractAgreement( stdctrl );
        stdctrl.cancel();
List<APXT_Redlining__Contract_Agreement__c> someList = testCombineAgree.GetAllRelatedAgreement();
      Test.Stoptest();

 

All Answers

Andrew GAndrew G
invoke the method against the page
Test.StartTest();
        //current page equal the legal entity page
        ApexPages.StandardController stdctrl = new ApexPages.StandardController( testEntity );
        ApexPages.currentPage().getParameters().put('id',testEntity.Id);
        CombineContractAgreement testCombineAgree = new CombineContractAgreement( stdctrl );
        stdctrl.cancel();
List<APXT_Redlining__Contract_Agreement__c> someList = testCombineAgree.GetAllRelatedAgreement();
      Test.Stoptest();

 
This was selected as the best answer
Upton_XUpton_X
yessss I messed around for hours trying to figure out that final bit!