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
Prabhat AroraPrabhat Arora 

Testing overriding functionality with visualforce extensions

Hi All,
Needed a help in testing the VF Extension,  I am overiding the OLI "EDIT" button to redirect to some custom VF page, below is my controller extension & VF page used for redirecting, 
<apex:page standardController="OpportunityLineItem" extensions="redirect_Ext" 
action="{!if(rCardUploaded==TRUE,'/apex/IRC_VF?oppId='+OpptyId,URLFOR($Action.OpportunityLineItem.Edit,OpportunityLineItem.Id,[
saveURL=OpportunityLineItem.Id,retURL=OpptyId],true))}">
</apex:page>
This VF Page is redirecting to another VF Page (IRC_VF) on meeting a check box true condition.
Below is the extension Controller.
public class redirect_Ext {
    public OpportunityLineItem opptyLineItemRec;
    Public Opportunity OpptyRec;
    Public String OpptyId{get;set;}
    public Boolean rCardUploaded{get;set;}
    public redirect_Ext(ApexPages.StandardController controller) {
        opptyLineItemRec = [select id, Imported_from_Rate_Card_c__c, OpportunityId from OpportunityLineItem where id =: System.currentPageReference().getParameters().get('id')];
        rCardUploaded = opptyLineItemRec.Import_from_R_Card_c__c;
        OpptyId = opptyLineItemRec.OpportunityId;

    }
}
Everything is Working perfect, I NEED HELP on Writing Test Class, as i am not able pass the pagerefernce or redirecting amongst the pages, i had created the test data, only need an understanding on how to cover testing page navigation in this scenario.
Help will be Highly appreciated.

 
Best Answer chosen by Prabhat Arora
Steven NsubugaSteven Nsubuga
Try this
FYI, in your apex class, you have Imported_from_Rate_Card_c__c and Import_from_R_Card_c__c
Is that correct? I was not sure which one to use in the test class, I went with Imported_from_Rate_Card_c__c.
 
@isTest
public class redirect_ExtTest {

    @isTest static void testredirect_Ext() {
		
        Test.startTest();
        Account testAccount = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1);
        insert testAccount;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating',
            LocaleSidKey = 'en_US',
            EmailEncodingKey  = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
        insert testOpportunity;
		
		
		Id pricebookId = Test.getStandardPricebookId();

		//Create your product
		Product2 prod = new Product2(
			 Name = 'Product X',
			 ProductCode = 'Pro-X',
			 isActive = true
		);
		insert prod;

		//Create your pricebook entry
		PricebookEntry pbEntry = new PricebookEntry(
			 Pricebook2Id = pricebookId,
			 Product2Id = prod.Id,
			 UnitPrice = 100.00,
			 IsActive = true
		);
		insert pbEntry;
		
		OpportunityLineItem oli = new OpportunityLineItem(
			 OpportunityId = testOpportunity.Id,
			 Quantity = 5,
			 Imported_from_Rate_Card_c__c = true,
			 PricebookEntryId = pbEntry.Id,
			 TotalPrice = quantity * pbEntry.UnitPrice
		);
		insert oli;
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(oli);
		
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(acct.Id));

        Test.setCurrentPage(pageRef);
		
		redirect_Ext redirectController = new redirect_Ext();
			
        Test.stopTest();
        System.assert(redirectController.opptyLineItemRec != null);
		System.assert(redirectController.OpptyId != null);
		System.assert(redirectController.rCardUploaded);
    }
    
}

 

All Answers

Steven NsubugaSteven Nsubuga
Try this
FYI, in your apex class, you have Imported_from_Rate_Card_c__c and Import_from_R_Card_c__c
Is that correct? I was not sure which one to use in the test class, I went with Imported_from_Rate_Card_c__c.
 
@isTest
public class redirect_ExtTest {

    @isTest static void testredirect_Ext() {
		
        Test.startTest();
        Account testAccount = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1);
        insert testAccount;
		
		Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating',
            LocaleSidKey = 'en_US',
            EmailEncodingKey  = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
        insert testOpportunity;
		
		
		Id pricebookId = Test.getStandardPricebookId();

		//Create your product
		Product2 prod = new Product2(
			 Name = 'Product X',
			 ProductCode = 'Pro-X',
			 isActive = true
		);
		insert prod;

		//Create your pricebook entry
		PricebookEntry pbEntry = new PricebookEntry(
			 Pricebook2Id = pricebookId,
			 Product2Id = prod.Id,
			 UnitPrice = 100.00,
			 IsActive = true
		);
		insert pbEntry;
		
		OpportunityLineItem oli = new OpportunityLineItem(
			 OpportunityId = testOpportunity.Id,
			 Quantity = 5,
			 Imported_from_Rate_Card_c__c = true,
			 PricebookEntryId = pbEntry.Id,
			 TotalPrice = quantity * pbEntry.UnitPrice
		);
		insert oli;
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(oli);
		
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(acct.Id));

        Test.setCurrentPage(pageRef);
		
		redirect_Ext redirectController = new redirect_Ext();
			
        Test.stopTest();
        System.assert(redirectController.opptyLineItemRec != null);
		System.assert(redirectController.OpptyId != null);
		System.assert(redirectController.rCardUploaded);
    }
    
}

 
This was selected as the best answer
Prabhat AroraPrabhat Arora
Thanks for the Help steven, i was doing the same, was only missing at, line 54 String.valueOf(o.Id)); value assignment.
thank you so much.