+ Start a Discussion
symantecAPsymantecAP 

need help with Test Class

Hi Here is my code and test class so far. please help me cover this as its not covering any class

public class ProductPriceBook23 {
    
   public class productInfo {
    
        public Product2 prod{get; set;}
        public List<PriceBookEntry> pbes {get; set;}
             

        public productInfo() {
            pbes = new List<PriceBookEntry>();
                        



        }
        
    }
    
    public List<ProductInfo> products {get; set;}
    
    public ProductPriceBook23() {
        products = new List<productInfo >();
        Map<Id, productInfo> pInfoMap = new Map<Id, productInfo>();
      for (PricebookEntry pEntry :[Select id, unitPrice, product2Id, product2.Name, product2.Description, product2.Family,product2.ATFE_Status__c, product2.ProductCode, pricebook2id, pricebook2.name from PricebookEntry where   product2.ATFE_Product_Region__c='NA' and product2.Family= 'PRODUCT' and pricebook2.IsActive=true and Pricebook2.name='NA LA (USD)'      LIMIT 1000  ]) {
            ProductInfo pInfo = new ProductInfo();
            if(!pInfoMap.containsKey(pEntry.product2Id)) {
                pInfo.prod = new Product2(id=pEntry.product2Id, Name=pEntry.product2.Name, Description=pEntry.product2.Description, Family=pEntry.product2.Family,ATFE_Status__c=pEntry.product2.ATFE_Status__c, ProductCode=pEntry.product2.ProductCode);
                pInfo.pbes.add(pEntry);

                pInfoMap.put(pEntry.product2Id, pInfo);
            } else {
                pInfo = pInfoMap.get(pEntry.product2Id);
                pInfo.pbes.add(pEntry);
            }
        }
 products = pInfoMap.values();
    }
     
    
}

 and the test class is 

 

@isTest

private class testpricebook{

private static testmethod void testpb(){

PriceBookEntry pbe1=[Select id,unitprice,Product2Id,Product2.Name,Pricebook2.name,
                        Product2.Description,Product2.Family from PriceBookEntry 
                         where product2.ATFE_Product_Region__c='NA' and product2.Family='PRODUCT'  limit 1]; 
Product2 prod = new Product2(Name='AT-iMG008GNB-10',Description='Grounded UPS without battery. Battery must be ordered separetly');

string pricebook = pbe1.Pricebook2.name;
//ProductPriceBook23.Productinfo('01t200000022vPnAAI','USD 47.00','AT-iMG008GNB-10',pricebook);
}



}

 

Please help

 

 

Damien_Damien_

You commented out your constructor for the class you wish to test.

//ProductPriceBook23.Productinfo('01t200000022vPnAAI','USD 47.00','AT-iMG008GNB-10',pricebook);

 

Shashikant SharmaShashikant Sharma

For creating test class you can look at structure of a Apex Test Class

 

http://forceschool.blogspot.com/search/label/Test%20Apex

 

Hope it will help you.

symantecAPsymantecAP

Yes coz it gives this error

 

Error: Compile Error: Method does not exist or incorrect signature: ProductPriceBook23.Productinfo(String, String, String, String) at line 13 column 1

Damien_Damien_

That's because it wasn't written correctly.  It's supposed to call a constructor, not a normal method.  I should have read closer, but ProductPriceBook23 doesn't have a method called Productinfo.

 

ProductPriceBook23 prodInfo = new ProductPriceBook23();

 

symantecAPsymantecAP

Hi Damien 

 

It worked. But it covers 80%. What possible things can be done to make it more durable

 

Thanks 

Adil 

Damien_Damien_

Create objects that will allow it to fall into all of the different if statements.

symantecAPsymantecAP

Can you give me a prototype of it. creating objects

Damien_Damien_

You already did it once.

 

Product2 prod = new Product2(Name='AT-iMG008GNB-10',Description='Grounded UPS without battery. Battery must be ordered separetly');
symantecAPsymantecAP

How do I accomplish for "IF" statements

Shashikant SharmaShashikant Sharma

If you want to test any class and any condition in it then your data should be such that the condition satisfies.

Ex : 

If i have a condition

 

if(Employee.Salary__C  > 25000)

 

then my test data should create a data where Salary for employee more than 25000 so that condition satisfies. Similarly you need to create data to satisfy your condition.

symantecAPsymantecAP

sure shashi lemme try this. .