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
Sunny NarulaSunny Narula 

Code Coveage : When value are dynamically picked using currentPage().getParameters()

Not getting the desired code coverage, need a trick to play around with this
I have a VF page that is providing a custom button on Standard Opportunity Page layout, and 
a custom controller that handles the click event (a method)
Here is the VF page and part of controller code

VF page
====== 
<apex:page showHeader="false" standardController="Opportunity" extensions="ctrlMultiClone" action="{!createClone}" />

Controller
======

public PageReference createClone(){          
        oppid = ApexPages.currentPage().getParameters().get('id');

        if(oppid != null)
        {
            List<Amenity_Billing__c> cons = new List<Amenity_Billing__c>();
            Opportunity opp = [SELECT ID, StageName, Profit_Center__c,CloseDate,Name,Notice_Status__c,AccountId, LeadSource, Rate_Type__c  FROM Opportunity WHERE Id = : oppid];
            
            Opportunity oppCopy = opp.clone(false,true);
            oppCopy.Name = oppCopy.Name +' '+ date.today(); 
            insert oppCopy;
       }
}

===============

Now when I  m running the functionlaity everything works as desired... i.e.
I click on Custom, it calls the method createClone() in custom controller, this line get the current opportunity Id of the page
oppid = ApexPages.currentPage().getParameters().get('id');
and creates the clone....

But... when I m running created a code coverage I make a call to createClone() from test class..
as I have no way to pass this oppid when called from test class 

requesting to please guide me a way out get "oopid" while creating a test class

thanks in advance
Sunny

Best Answer chosen by Sunny Narula
Balaji BondarBalaji Bondar
Hi Sunny ,

Use code below :
// create an opp
    Opportunity opp = new Opportunity(name='testOpp');
    opp.stageName = 'Closed Won';
    opp.CloseDate = Date.today() - 4;
	insert opp;

PageReference pgRef = Page.Appt_New; //Create Page Reference - 'Appt_New' is the name of Page
ApexPages.currentPage().getParameters().put('id', opp.id);//Pass Id to page

 

All Answers

Balaji BondarBalaji Bondar
Hi Sunny ,

Use code below :
// create an opp
    Opportunity opp = new Opportunity(name='testOpp');
    opp.stageName = 'Closed Won';
    opp.CloseDate = Date.today() - 4;
	insert opp;

PageReference pgRef = Page.Appt_New; //Create Page Reference - 'Appt_New' is the name of Page
ApexPages.currentPage().getParameters().put('id', opp.id);//Pass Id to page

 
This was selected as the best answer
Sunny NarulaSunny Narula

Thanks Balaji :) 

I was creating Opportunity but this line is really helpful.

ApexPages.currentPage().getParameters().put('id', opp.id);