+ Start a Discussion
devloper sfdcdevloper sfdc 

code coverage problem in test class

Hi All 
 I have created test class but it is not cover  code coverage please check what the issue in my test class . 

test class and  class is here-

public class ProductEntry{ 
public List<sObjectWrapper> wrappers{get;set;} 
Public string selectedname{get;set;}
public String descr{get;set;}
public  Decimal Qty{get;set;}                                                                             
public boolean isSelected{get;set;}
public boolean flag=true;
public boolean Oppflag=true;
public boolean flag1{get;set;}
public id Oppid {get;set;}
public List<PricebookEntry> PriceEntry{get;set;}
public List<OpportunityLineItem>Optyline{get;set;}
  public ProductEntry(ApexPages.StandardController stdController) {
        PriceEntry=new List<PricebookEntry>();
      wrappers=new List<sObjectWrapper>();
     
 Optyline=new List<OpportunityLineItem>();
 
      flag1=true;
  }

   public void hideSection()
    {
    if(flag1==true)
    {
        flag1=false;
    }
        else {flag1=true;}
    }
        
    
   
 
 public List<sObjectWrapper> getData()
 {
     flag=true;
     wrappers.clear();
   
 
  Set<Id> productIds = New Set<Id>();  
 Set<Id>  PriceEntryIds= new Set<Id>();
 Set<Id>  PriceBookIds= new Set<Id>(); 
 List<sObjectWrapper> wrapper = new List<sObjectWrapper>(); 
for(Product2 p : [select id,name,Units_of_Measure__c from product2 where Bundle_Type__c=:selectedname]){
  
productIds.add(p.Id);
}     
     for(PriceBook2 pb:[select id from pricebook2 where name In('BlackBeltHelp V2')])
         {
          PriceBookIds.add(pb.id);  
         }
     
  
           for(PriceBookEntry pp:[SELECT id,Name,FFUnit_of_measure__c,Qty__c,Pricebook2Id,Product2Id,ProductCode,UnitPrice,UseStandardPrice FROM PricebookEntry WHERE Pricebook2Id=:PriceBookIds and product2Id=:productIds])
           {
               wrappers.add(new sObjectWrapper(pp,true));
              
       }
  
    return  null;
 }
    public void addlist()
    {   
        PriceEntry.clear();
        
       
         for(sObjectWrapper wrap : wrappers){  
       /*Check if record is selected*/  
       if(wrap.isSelected){
         
           
         PriceEntry.add(wrap.prod);  
        
    }
    }  
    }
    
 public void AddtoOpty()
{  
    addlist();
   if(flag==true)
   { 
      for(PricebookEntry prc:PriceEntry)
      {
      
      OpportunityLineItem opt=new OpportunityLineItem();
       Oppid = apexpages.currentpage().getparameters().get('id');
      
     opt.OpportunityId=Oppid;
     opt.PricebookEntryId=prc.id;
      opt.Unit_of_measure__c=prc.FFUnit_of_measure__c;
       
       opt.Product2Id=prc.product2id;
          opt.UnitPrice=prc.UnitPrice;        
         
              opt.Quantity=prc.Qty__c; 
          opt.Description=descr; 
          
          Optyline.add(opt);
      }
                  
      // insert optyline;
       PriceEntry.clear();
         flag=false;                 
}    
   
}  

   public void addtOpp()
   { 
       if(Oppflag==true)
   {
      if(Optyline.size()>0) 
      {
          insert Optyline; 
      } 
     
   } Oppflag=false;
       
   } 
 //   
public pagereference urlre()
{
Oppid = apexpages.currentpage().getparameters().get('id');
pagereference pg =new pagereference('/'+Oppid);
//pg.setRedirect(true);
return pg;   
}
   
   // 
    Public List<string> optionList=new List<String>{'Basic','Advanced','Premium'};
        Public List<Selectoption> getselectedaccnamefields(){
            List<Selectoption> lstnamesel = new List<selectoption>();
            lstnamesel.add(new selectOption('', '-All Products-'));
            for(String s :optionList){
            lstnamesel.add(new selectoption(s,s));
            }
            return lstnamesel; 
        }
    public class sObjectWrapper
    {
    public boolean isSelected{get;set;}  
    public PricebookEntry prod{get;set;} 
        
    public sObjectWrapper(PricebookEntry prod,Boolean isSelected){  
    this.prod = prod;      
    this.isSelected = isSelected;  
     }  
    }
}

and my test class is 
@isTest
public class ProductEntryTest {
    
  

Public  static testMethod void test()
{
    List<opportunityLineItem> Oli=new List<opportunityLineItem>();
    Account acc=new Account();
    acc.name='MassBay';
    insert acc;
    Opportunity opp=new Opportunity();
    string p;
opp.CloseDate=date.today().addMonths(2);
opp.Name='test opp';
opp.StageName='IsWon';
opp.AccountId=acc.id;
    
insert opp;
        
        
     Product2 prd1 = new Product2 (); // ----> Create  product
    prd1.Name='Accomodation';
    prd1.Units_of_Measure__c='1';
    insert prd1;
     Id pricebookId = Test.getStandardPricebookId();
    
    PricebookEntry standardPrice = new PricebookEntry(
    Pricebook2Id = pricebookId, Product2Id = prd1.Id,
    UnitPrice = 10000, IsActive = true);
   insert standardPrice;

Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;

PricebookEntry customPrice = new PricebookEntry(
    Pricebook2Id = customPB.Id, Product2Id = prd1.Id,
    UnitPrice = 12000, IsActive = true);
insert customPrice;
    
    
    
    
  
    OpportunitylineItem otm=new Opportunitylineitem();
    otm.OpportunityId=opp.id;
    otm.PricebookEntryId=standardPrice.id;
    otm.Product2Id=prd1.id;
    otm.Unit_of_measure__c=prd1.Units_of_Measure__c;
    oli.add(otm);
if(oli.size()>1)
{
    insert oli;
        
}  
}
}
devloper sfdcdevloper sfdc
Please Help Me
kd Komalkd Komal
Hi
Could you please share the VF code as well ?
kd Komalkd Komal
Hi 
Also try adding the below code in your test method

PageReference pageRef = Page.PageName;
Test.setCurrentPage(pageRef);

Replace pageName with your VF page name