You need to sign in to do that
Don't have an account?
CRM Project
Test Case for Wrapper Class - How to get mere then 75%
I am trying to write a test class for my Wrapper Class, unfortunately my overall code coverage is 51%, so I need to get more then 75% in order to deploy.how i will do plz help me
public with sharing class CRM_AddProducts
{
public String message { get; set; }
public String SelectedProduct{get;set;}
public list<pProduct> lstproduct { get; set; }
public list<cproduct> cartlstproduct {get;set;}
public list<CProduct__c> lstselproduct { get; set; }
public list<CQuote_Line_Item__c> lstaddquoteproduct { get; set; }
public List<SelectOption> Product_Selectionoptions {get;set;}
public boolean selected {get;set;}
public boolean display {get;set;}
public CProduct__c pro {get;set;}
// public CProduct__c cpro{get;set;}
// public CProduct__c cess{get;set;}
public map<Id,double> mapProductIdToQuantity { get; set; }
public string strid;
public boolean blnStatus1 ;
public CRM_AddProducts()
{
lstproduct=new list<pProduct>();
mapProductIdToQuantity = new map<Id,double>();
cartlstproduct = new list<cproduct>();
lstaddquoteproduct = new list<CQuote_Line_Item__c>();
strid = apexpages.currentPage().getParameters().get('id');
for(CProduct__c objproduct : [select id,
Name,
List_Price__c,
Product_Code__c,
Product_Description__c,
Sales_Price__c,
Product_Family__c
from CProduct__c])
{
lstproduct.add(new pProduct(objproduct));
system.debug('---lstproduct---'+lstproduct);
}
}
public pageReference check()
{
return null;
}
//builds a picklist of product names based on their product id
public List<SelectOption>getProduct_Selection()
{
List<SelectOption> Product_Selectionoptions = new List<SelectOption>();
//new list for holding all of the picklist options
for (CProduct__c Product_Selection : [SELECT Id, Name FROM CProduct__c])
{
//query for Product records
Product_Selectionoptions.add(new selectOption(Product_Selection.Id,Product_Selection.Name));
//for all records found - add them to the picklist options
}
system.debug('-Product_Selectionoptions-'+Product_Selectionoptions);
return Product_Selectionoptions; //return the picklist options
}
public pageReference addtocart()
{
for(pProduct cProd : lstproduct)
{
system.debug('----cProd---'+cProd);
system.debug('--SelectedProduct--'+SelectedProduct);
system.debug('-Product_Selectionoptions-'+Product_Selectionoptions);
// system.debug('-Product_Selection-'+Product_Selectionoptions.Product_Selection);
if(cProd.selected == true)
{
system.debug('----cProd---'+cProd);
CProduct__c objproduct1 = [ select id, Name,
List_Price__c,
Product_Code__c,
Product_Description__c,
Sales_Price__c,
Product_Family__c
from CProduct__c
where id =:SelectedProduct];
cartlstproduct.add(new cproduct(objproduct1)) ;
system.debug('---cartlstproduct---'+cartlstproduct);
}
}
return null ;
}
public pageReference processSelected()
{
list<CQuote_Line_Item__c> lstaddquoteproduct = new list<CQuote_Line_Item__c>();
// list<CProduct__c> lstselproduct = new list<CProduct__c>();
system.debug(lstproduct+'===lstproduct====');
for(cproduct cess:cartlstproduct)
{
system.debug('-----cess---'+cess);
if(cess.Display== true)
{
if((cess.quantity!=null && cess.quantity!='' && cess.quantity!=string.valueof(0) )&& (cess.salesprice!=null && cess.salesprice!='' ) )
{
//mapProductIdToQuantity.put(cPro.pro.id,double.valueof(cPro.quantity));
//system.debug(mapProductIdToQuantity+'---map---');
CQuote_Line_Item__c objQuoteli = new CQuote_Line_Item__c();
system.debug('---cess.quantity---'+cess.quantity);
objQuoteli.Name = cess.cpro.Name ;
objQuoteli.Quantity__c =double.valueOf(cess.quantity) ;
objQuoteli.Unit_Price__c =cess.cpro.List_Price__c;
objQuoteli.Quote__c = strid;
objQuoteli.Total_Amount__c= double.valueof(cess.TotalAmount );
//objQuoteli.Discount_Percent__c = double.valueOf(cess.discount) ;
objQuoteli.Sales_Price__c = double.valueOf(cess.salesprice) ;
system.debug('---objQuoteli---'+objQuoteli);
lstaddquoteproduct.add(objQuoteli);
system.debug(lstaddquoteproduct+'----lstaddquoteproduct----');
}
else
{
system.debug('---else---');
ApexPages.Message errormessage = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter all the values.');
ApexPages.addMessage(errormessage);
}
}
}//for loop
if(lstaddquoteproduct.size()>0)
{
insert lstaddquoteproduct;
system.debug('---strid--'+strid);
pageReference pageRef1 = new pageReference('/'+strid);
pageRef1.setredirect(true);
return pageRef1;
}
return null;
}
//product list wrapper class
public class pProduct
{
public CProduct__c pro {get;set;}
public boolean selected {get;set;}
public string quantity {get;set;}
public string salesprice {get;set;}
public integer TotalAmount {get;set;}
public integer discount {get;set;}
//public string discount {get;set;}
public pProduct(CProduct__c p)
{
pro = p;
selected = false ;
quantity='1';
}
}
//cart list wrapper class
public class cproduct
{
public CProduct__c cpro{get;set;}
public boolean selected {get;set;}
public boolean Display {get;set;}
public string quantity {get;set;}
public string salesprice {get;set;}
public string TotalAmount {get;set;}
public string discount {get;set;}
public cProduct(CProduct__c c)
{
cpro = c;
selected = false ;
quantity='1';
salesprice= string.valueof( cpro.List_Price__c );
TotalAmount = string.valueof( cpro.List_Price__c );
Display = false ;
}
}
public pageReference cancelbutton()
{
pageReference pageRef1 = new pageReference('/'+strid);
pageRef1.setredirect(true);
return pageRef1;
}
}
public with sharing class CRM_AddProducts
{
public String message { get; set; }
public String SelectedProduct{get;set;}
public list<pProduct> lstproduct { get; set; }
public list<cproduct> cartlstproduct {get;set;}
public list<CProduct__c> lstselproduct { get; set; }
public list<CQuote_Line_Item__c> lstaddquoteproduct { get; set; }
public List<SelectOption> Product_Selectionoptions {get;set;}
public boolean selected {get;set;}
public boolean display {get;set;}
public CProduct__c pro {get;set;}
// public CProduct__c cpro{get;set;}
// public CProduct__c cess{get;set;}
public map<Id,double> mapProductIdToQuantity { get; set; }
public string strid;
public boolean blnStatus1 ;
public CRM_AddProducts()
{
lstproduct=new list<pProduct>();
mapProductIdToQuantity = new map<Id,double>();
cartlstproduct = new list<cproduct>();
lstaddquoteproduct = new list<CQuote_Line_Item__c>();
strid = apexpages.currentPage().getParameters().get('id');
for(CProduct__c objproduct : [select id,
Name,
List_Price__c,
Product_Code__c,
Product_Description__c,
Sales_Price__c,
Product_Family__c
from CProduct__c])
{
lstproduct.add(new pProduct(objproduct));
system.debug('---lstproduct---'+lstproduct);
}
}
public pageReference check()
{
return null;
}
//builds a picklist of product names based on their product id
public List<SelectOption>getProduct_Selection()
{
List<SelectOption> Product_Selectionoptions = new List<SelectOption>();
//new list for holding all of the picklist options
for (CProduct__c Product_Selection : [SELECT Id, Name FROM CProduct__c])
{
//query for Product records
Product_Selectionoptions.add(new selectOption(Product_Selection.Id,Product_Selection.Name));
//for all records found - add them to the picklist options
}
system.debug('-Product_Selectionoptions-'+Product_Selectionoptions);
return Product_Selectionoptions; //return the picklist options
}
public pageReference addtocart()
{
for(pProduct cProd : lstproduct)
{
system.debug('----cProd---'+cProd);
system.debug('--SelectedProduct--'+SelectedProduct);
system.debug('-Product_Selectionoptions-'+Product_Selectionoptions);
// system.debug('-Product_Selection-'+Product_Selectionoptions.Product_Selection);
if(cProd.selected == true)
{
system.debug('----cProd---'+cProd);
CProduct__c objproduct1 = [ select id, Name,
List_Price__c,
Product_Code__c,
Product_Description__c,
Sales_Price__c,
Product_Family__c
from CProduct__c
where id =:SelectedProduct];
cartlstproduct.add(new cproduct(objproduct1)) ;
system.debug('---cartlstproduct---'+cartlstproduct);
}
}
return null ;
}
public pageReference processSelected()
{
list<CQuote_Line_Item__c> lstaddquoteproduct = new list<CQuote_Line_Item__c>();
// list<CProduct__c> lstselproduct = new list<CProduct__c>();
system.debug(lstproduct+'===lstproduct====');
for(cproduct cess:cartlstproduct)
{
system.debug('-----cess---'+cess);
if(cess.Display== true)
{
if((cess.quantity!=null && cess.quantity!='' && cess.quantity!=string.valueof(0) )&& (cess.salesprice!=null && cess.salesprice!='' ) )
{
//mapProductIdToQuantity.put(cPro.pro.id,double.valueof(cPro.quantity));
//system.debug(mapProductIdToQuantity+'---map---');
CQuote_Line_Item__c objQuoteli = new CQuote_Line_Item__c();
system.debug('---cess.quantity---'+cess.quantity);
objQuoteli.Name = cess.cpro.Name ;
objQuoteli.Quantity__c =double.valueOf(cess.quantity) ;
objQuoteli.Unit_Price__c =cess.cpro.List_Price__c;
objQuoteli.Quote__c = strid;
objQuoteli.Total_Amount__c= double.valueof(cess.TotalAmount );
//objQuoteli.Discount_Percent__c = double.valueOf(cess.discount) ;
objQuoteli.Sales_Price__c = double.valueOf(cess.salesprice) ;
system.debug('---objQuoteli---'+objQuoteli);
lstaddquoteproduct.add(objQuoteli);
system.debug(lstaddquoteproduct+'----lstaddquoteproduct----');
}
else
{
system.debug('---else---');
ApexPages.Message errormessage = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter all the values.');
ApexPages.addMessage(errormessage);
}
}
}//for loop
if(lstaddquoteproduct.size()>0)
{
insert lstaddquoteproduct;
system.debug('---strid--'+strid);
pageReference pageRef1 = new pageReference('/'+strid);
pageRef1.setredirect(true);
return pageRef1;
}
return null;
}
//product list wrapper class
public class pProduct
{
public CProduct__c pro {get;set;}
public boolean selected {get;set;}
public string quantity {get;set;}
public string salesprice {get;set;}
public integer TotalAmount {get;set;}
public integer discount {get;set;}
//public string discount {get;set;}
public pProduct(CProduct__c p)
{
pro = p;
selected = false ;
quantity='1';
}
}
//cart list wrapper class
public class cproduct
{
public CProduct__c cpro{get;set;}
public boolean selected {get;set;}
public boolean Display {get;set;}
public string quantity {get;set;}
public string salesprice {get;set;}
public string TotalAmount {get;set;}
public string discount {get;set;}
public cProduct(CProduct__c c)
{
cpro = c;
selected = false ;
quantity='1';
salesprice= string.valueof( cpro.List_Price__c );
TotalAmount = string.valueof( cpro.List_Price__c );
Display = false ;
}
}
public pageReference cancelbutton()
{
pageReference pageRef1 = new pageReference('/'+strid);
pageRef1.setredirect(true);
return pageRef1;
}
}
I have modifed tthe above mentioned class please chek whether it running fine for you or not
Thanks
Sunil
All Answers
@isTest
public class CRM_AddProducts_Test
{
static testMethod void myUnitTest()
{
PageReference pageRef = Page.CRM_AddProducts;
Test.setCurrentPage(pageRef);
//String SelectedProduct;
List<SelectOption> Product_Selectionoptions = new List<SelectOption>();
boolean selected;
boolean Display;
string quantity;
String message;
string salesprice;
integer TotalAmount;
integer discount;
selected = false;
quantity ='test';
CProduct__c cpro = new CProduct__c();
//boolean selected;
//boolean Display;
// string quantity;
//string salesprice;
// string TotalAmount;
// string discount;
CProduct__c objproduct=new CProduct__c();
objproduct.Name='Test data';
objproduct.List_Price__c=1234;
ApexPages.CurrentPage().getparameters().put('id',objproduct.id);
string cid=ApexPages.currentPage().getParameters().get('id');
objproduct.Product_Description__c='Test';
objproduct.Sales_Price__c=1234;
insert objproduct;
List<CProduct__c> lstcon = new List<CProduct__c>();
lstcon.add(objproduct);
CProduct__c objproduct1=new CProduct__c();
objproduct1.Name='Test data';
objproduct1.List_Price__c=1234;
ApexPages.CurrentPage().getparameters().put('id',objproduct1.id);
string cid1=ApexPages.currentPage().getParameters().get('id');
objproduct1.Product_Description__c='Test';
objproduct1.Sales_Price__c=1234;
insert objproduct1;
List<CProduct__c> lstcon2 = new List<CProduct__c>();
lstcon2.add(objproduct1);
//CProduct__c obj1 = new CProduct__c();
//obj1= [SELECT Id, Name FROM CProduct__c];
// List<SelectOption> Product_Selectionoptions = new List<SelectOption>();
//ApexPAges.StandardController sc1 = new ApexPages.StandardController(obj1);
//Product_Selectionoptions.add(obj1);
//list<pProduct> lstproduct=new list<pProduct>();
//List<cproduct> cartlstproduct = new List<cproduct>();
Account acc = new Account();
acc.Name = 'testname';
acc.Email__c = 'abcemail@gmail.com';
acc.Mobile__c= '7446760472';
acc.Phone = '7446760472';
acc.Website='www.googleyajoo.com';
//acc.BillingAddress='Bangalore';
//acc.City__c='Bangalore';
insert acc;
Quote__c objQuote=new Quote__c();
objQuote.Name__c ='quotename';
objQuote.Discount__c=10;
objQuote.account__c=acc.id;
objQuote.Discount_Status__c='Open';
objQuote.Total_Amount__c=100;
objQuote.Contact_Phone__c='7676716272';
//objQuote.Billing_City__c='Bangalore';
//objQuote.Billing_Country__c='India';
insert objQuote;
CQuote_Line_Item__c objQli=new CQuote_Line_Item__c();
objQli.Name='Test Name';
objQli.Quote__c=objQuote.id;
objQli.Unit_Price__c=123;
objQli.Quantity__c=1;
objQli.Quote__c = objQuote.id ;
insert objQli;
List<CQuote_Line_Item__c> lstcon1 = new List<CQuote_Line_Item__c>();
lstcon1.add(objQli);
ApexPAges.StandardController sc = new ApexPages.StandardController(objproduct);
//ApexPAges.StandardController sc1 = new ApexPages.StandardController(objproduct1);
CRM_AddProducts crm = new CRM_AddProducts();
//CRM_AddProducts crm1 = new CRM_AddProducts();
CRM_AddProducts.pProduct Crmp = new CRM_AddProducts.pProduct(objproduct);
//CRM_AddProducts.cproduct Crmc = new CRM_AddProducts.pProduct(objproduct1);
crm.check();
crm.getProduct_Selection();
crm.addtocart();
crm.processSelected();
crm.cancelbutton();
/*crm1.check();
crm1.getProduct_Selection();
crm1.addtocart();
crm1.processSelected();
crm1.cancelbutton();*/
}
}
plz check ...................
Please check below blog for more information on test classes
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please let us know if this will help you
Thanks,
Amit Chaudhary
I have modifed tthe above mentioned class please chek whether it running fine for you or not
Thanks
Sunil
Thanks
Sunil
if((cess.quantity!=null && cess.quantity!='' && cess.quantity!=string.valueof(0) )&& (cess.salesprice!=null && cess.salesprice!='' ) )
{
//mapProductIdToQuantity.put(cPro.pro.id,double.valueof(cPro.quantity));
//system.debug(mapProductIdToQuantity+'---map---');
CQuote_Line_Item__c objQuoteli = new CQuote_Line_Item__c();
system.debug('---cess.quantity---'+cess.quantity);
objQuoteli.Name = cess.cpro.Name ;
objQuoteli.Quantity__c =double.valueOf(cess.quantity) ;
objQuoteli.Unit_Price__c =cess.cpro.List_Price__c;
objQuoteli.Quote__c = strid;
objQuoteli.Total_Amount__c= double.valueof(cess.TotalAmount );
//objQuoteli.Discount_Percent__c = double.valueOf(cess.discount) ;
objQuoteli.Sales_Price__c = double.valueOf(cess.salesprice) ;
system.debug('---objQuoteli---'+objQuoteli);
lstaddquoteproduct.add(objQuoteli);
system.debug(lstaddquoteproduct+'----lstaddquoteproduct----');
}
plz check ......
I am not sure about this object as this is custom object but for the clarifition you can put debugs before if condition and check that whether values are coing or not
Then you can verify that you are on right path or not