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
domdickdomdick 

Need help with test coverage on apex class

Hello,

 

I have apex controller to check if opportunity has a pricebook associated yet or not. It works fine as user point of view but strugling to make a test class for this.

 

Much appriciate any help with an example!

 

thanks

 

 

 

AdrianCCAdrianCC

Yes, I had this problem as well when working with PriceBooks. I usually use seeAllData=true and use the existing records to simplify things. Make sure that the records that you are using in the SandBox also exist in Production....

 

Happy Friday,

Adrian

domdickdomdick

Thanks Adrian for the inupt on this.

 

Here is the test but still doesn't get full coverage.

 

I am sure something missing, so further help required. Thanks,

 

@IsTest(SeeAllData=true)
private class ProductServicesTest {
    
    static testMethod void myUnitForOpportunityTest() {
    	
    	Pricebook2 pb = [Select Id, Name From Pricebook2 Where IsStandard = true Limit 1];
    	/* Setup a basic opportunity */
        Account act = [SELECT Id from Account LIMIT 1];
        Opportunity o  = new Opportunity();
        o.Name         = 'TEST';
        o.AccountId    = act.Id;
        o.CloseDate    = Date.today();
        o.StageName    = 'Interest';
        o.Primary_Contact_Assigned__c =True;
        //o.Pricebook2Id = pb.id;      

        Database.insert(o);
    	
    	PageReference pageRef = Page.ProductServicesDemo;
        pageRef.getParameters().put('Id',o.Id);
        Test.setCurrentPageReference(pageRef);
        
        ProductServices proServices = new ProductServices(new ApexPages.StandardController(o));
        System.assert(proServices.priceBookCheck()!=null);
        proServices.changePricebook();      
        
    }
}

 

Vinit_KumarVinit_Kumar

Add below line to your code :-

 

proServices.priceBookCheck();  

 as you are not testing priceBookCheck() method in your test class.

domdickdomdick

i have already tried to add this line with other test but it doesn't cover the following lines from initial test...

 any other ideas?

 

theBook = activepbs[0];
System.debug('********************Active PBS Pricebook2***************' +theBook);

also

try {
theOpp.Pricebook2Id = theBook.Id;
update(theOpp);
}
catch(Exception e) {
ApexPages.addMessages(e);
}

 

AdrianCCAdrianCC

Well, this all seems to be related to the your specific test data. You need to set up testing conditions for all your if branches... You could use the  the viewalldata annotation only for one case, and create a new test method that doesn't have that one and build your own test data.

 

For the :

try {
theOpp.Pricebook2Id = theBook.Id;
update(theOpp);
}
catch(Exception e) {
ApexPages.addMessages(e);
}

 part you could make that forcepricebookselection public and set it to different values from your test method before running its related method.

 

Happy Monday!

Adrian