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
hylim1215hylim1215 

please help my test method on custom extension

Hi i have a custom exten for my Quote object as below:
 
public with sharing class quote_NewQuoteExtension{
    ApexPages.StandardController controller;
    public Opportunity opp2 {get; set;}
    public Account acc {get; set;}
    public Contact ctc {get; set;}
    private Quote qo;

public quote_NewQuoteExtension(ApexPages.StandardController stdController){

controller = stdController;
        qo = (Quote)stdController.getRecord();
        
        oid = System.currentpageReference().getParameters().get('oppid');

if(opp2 == null){
            opp2 = [Select id, Pricebook2Id,name, accountid, Type_of_Quote__c, Contact_Name__c from opportunity where id =: oid];
        }

.
..
}
and my test method always show list has no row on opp2 list. my test unit
 
public static testMethod void MainTestClass(){

account acc = new account(name='xx' ..);

insert acc;

contact ctc = new contact(name =  'xx'..);
insert ctc;

opportunity opp = new opportunity(name = 'xx'..);
insert opp;

quote qo = new quote(name='xx');
insert qo;

 ApexPages.StandardController sc = new ApexPages.StandardController(quo);
 quote_NewQuoteExtension testNewQuoteExt = new quote_NewQuoteExtension(sc);
        
        PageReference pageRef = Page.quote_NewQuoteMaster;
        Test.setCurrentPage(pageRef);
        testNewQuoteExt.opp2 = opp;
        testNewQuoteExt.acc = acc;
        testNewQuoteExt.ctc = ctc;
        testNewQuoteExt.save();

}
i have already set opp2 why still null?

Thanks.
Nagendra ChinchinadaNagendra Chinchinada
You have to add opportunity id to URL, since oid is beeing taken from System.currentpageReference().getParameters().get('oppid');
Add this line to ur test class,
ApexPages.currentPage().getParameters().put('oppid',opp.Id);
 
public static testMethod void MainTestClass(){

account acc = new account(name='xx' ..);

insert acc;

contact ctc = new contact(name =  'xx'..);
insert ctc;

opportunity opp = new opportunity(name = 'xx'..);
insert opp;

quote qo = new quote(name='xx');
insert qo;

 ApexPages.StandardController sc = new ApexPages.StandardController(quo);
 quote_NewQuoteExtension testNewQuoteExt = new quote_NewQuoteExtension(sc);
 
 ApexPages.currentPage().getParameters().put('oppid',opp.Id);// Add opp id to URL
        
        PageReference pageRef = Page.quote_NewQuoteMaster;
        Test.setCurrentPage(pageRef);
        //testNewQuoteExt.opp2 = opp;
        testNewQuoteExt.acc = acc;
        testNewQuoteExt.ctc = ctc;
        testNewQuoteExt.save();

}

Thanks,
Nagendra
hylim1215hylim1215
Hi Nagendra,

i tried but it sill return error "list has no row returned". Any idea?

Thanks