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
GemMaddyGemMaddy 

TestClass to test product search

Hi,

 

I've a code where I search for the products. To write a independent test, I need to create product and its corresponding entries like pricebook, standard price etc. To create a standard price, I tried the following code

 

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

 

But as I don't have set the SeeAllData annotation to true, this return nothing. I can't create the same new as IsStandard is read-only field.

 

Can anybody advise me with an alternate approach, probably where I can set the annotation SeeAllData to method level only (where I'm creating the product)?

 

Regards

Manish

Vinit_KumarVinit_Kumar

Hi Gem,

 

Please try below code to create a Test class:-

 

@IsTest(SeeAllData=true)
public class Pricebook_test
{

    static testmethod void myTestMethod() 
    {
        Pricebook2 standardPB = [select name, isactive from Pricebook2 where IsStandard = true limit 1];
        
        Pricebook2 prb= new  Pricebook2(Name='Test',Description ='Price Book 2009 Products',IsActive = true);
        insert prb;
        
        Product2 pr = new Product2(Name='Test',IsActive=true);
        insert pr;
        
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = pr.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = prb.Id, Product2Id = pr.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;

 

}

 

}

GemMaddyGemMaddy

Hi Vinit,

 

I agree with this approach, but I'm really not keen to use the annotation for all my tests. As I can't create a Pricebook entry for IsStandard field, I'm looking to restrict the SeeAllData availability to method level only. Putting it on class level defeats the purpoase of having all data created for unit testing.

 

Regards

Manish

Vinit_KumarVinit_Kumar

Manish,

 

I am afraid that you have that on method level,I have not seen as such till now.