You need to sign in to do that
Don't have an account?

issue in test class please help
Hello Frinds ,
please help me to wirte test class for below code .
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 Integer rowNo {set;get;}
public boolean isSelected{get;set;}
public boolean flag=true;
public boolean Oppflag=true;
public boolean flag1{get;set;}
public id Oppid {get;set;}
public string bundle{get;set;}
public Map<String,String> Bundleprice {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>();
Bundleprice=new map<String,String>();
Optyline=new List<OpportunityLineItem>();
// Unit_of_Measure=new List<String>();
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('One Stop Shop')])
{
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','Add-Ons'};
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;
}
}
}
please help me to wirte test class for below code .
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 Integer rowNo {set;get;}
public boolean isSelected{get;set;}
public boolean flag=true;
public boolean Oppflag=true;
public boolean flag1{get;set;}
public id Oppid {get;set;}
public string bundle{get;set;}
public Map<String,String> Bundleprice {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>();
Bundleprice=new map<String,String>();
Optyline=new List<OpportunityLineItem>();
// Unit_of_Measure=new List<String>();
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('One Stop Shop')])
{
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','Add-Ons'};
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;
}
}
}
All Answers
It is not working
i have done chages according to error but problem is same .
i have done changes
opportunityLineItem o=new opportunityLineItem();
o.Unit_of_measure__c='100';
o.OpportunityId=opp1.id;
o.PricebookEntryId=customPrice.Id;
o.Product2Id=prod.id;
insert o;
Still your getting the Same Error or some new error:
The Error is due to while you Product in the Salesforce you are having the Units_of_Measure__c as required field in the Product object.
so please use the below small updated code:
@istest
public class apexTest
{
static testmethod void testM(){
Test.StartTest();
Account a = new Account();
a.Name = 'Test Account';
insert a;
Opportunity opp = new Opportunity();
opp.Name='NewOpp';
opp.AccountId=a.Id;
opp.StageName='Prospecting';
opp.CloseDate=Date.today().addDays(10);
insert opp;
Product2 prod = new Product2(Name = 'One Stop Shop',
Family = 'Hardware', Units_of_Measure__c = '100');
insert prod;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry(
Pricebook2Id = pricebookId, Product2Id = prod.Id,
UnitPrice = 10000, IsActive = true);
insert standardPrice;
Pricebook2 customPB = new Pricebook2(Name='One Stop Shop', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(
Pricebook2Id = customPB.Id, Product2Id = prod.Id,
UnitPrice = 12000, IsActive = true);
insert customPrice;
Opportunity opp1= new Opportunity(Name='Test',
NextStep='test',
PriceBook2 = customPB,
CloseDate = System.today(),
AccountId=a.Id,
StageName='Closed Won - One Time',
Probability=100);
insert opp1;
opportunityLineItem o=new opportunityLineItem();
o.OpportunityId=opp1.id;
o.PricebookEntryId=customPrice.Id;
o.Product2Id=prod.id;
insert o;
ApexPages.StandardController sc = new ApexPages.StandardController(opp1);
ProductEntry controller = new ProductEntry(sc);
ProductEntry.sObjectWrapper wrapper = new ProductEntry.sObjectWrapper(customPrice ,true);
controller.hideSection();
controller.getData();
controller.addtOpp();
controller.getselectedaccnamefields();
controller.addlist();
controller.AddtoOpty();
controller.urlre();
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj