You need to sign in to do that
Don't have an account?
Shruthi Narsi
test Class Code coverage 70%
I have wrotten the below code to add quoteline items . The code covergae is only 70%. Can you help me with the code coverage tp be 90%
Test Class
@isTest
Private class TestProductSearchpopupController {
static testMethod void method() {
Account acc = new Account ();
acc.Name = 'Test1';
insert acc;
Opportunities__c opp= new Opportunities__c ();
opp.Name= 'Testopp1';
opp.Account_Name__c = acc.id;
opp.Close_Date__c = date.today();
opp.Stage__c = 'Prospecting';
insert opp;
Quotes__c testque = new Quotes__c();
testque.Name = 'Test PriceBookEntry';
testque.OpportunityId__c = opp.id;
testque.AccountId__c = acc.id;
insert testque;
List <Product2__c> lstproduct = new List<Product2__c>();
Product2__c testProduct = new Product2__c();
testProduct.Name='Test product';
lstproduct.add(testProduct);
insert lstproduct;
List <PricebookEntry__c> lstpbe = new List<PricebookEntry__c>();
PricebookEntry__c testpbe = new PricebookEntry__c();
testpbe.Name='Test';
lstpbe.add(testpbe);
insert lstpbe;
product2 pr = new product2();
pr.name = 'Test product';
insert pr;
List<Pricebook2> pricebook2List = new List<pricebook2>();
PriceBook2 pb2=new PriceBook2();
pb2.Name = 'test';
pb2.IsActive = true;
pricebook2List.add(pb2);
insert pricebook2List;
PricebookEntry two = new PricebookEntry();
two.pricebook2Id = Test.getStandardPricebookId();
two.product2id = pr.id;
two.unitprice = 1249.0;
two.isactive = true;
insert two;
PricebookEntry one = new PricebookEntry();
one.pricebook2Id = pb2.id;
one.product2id = pr.id;
one.unitprice = 1249.0;
one.isactive = true;
insert one;
List <QuoteLineitem__c> lstqli = new List<QuoteLineitem__c>();
QuoteLineitem__c testqli = new QuoteLineitem__c();
testqli.QuotesId__c = testque.id;
testqli.Quantity__c = 12;
lstqli.add(testqli);
insert lstqli;
PageReference pageRef = Page.ProductSearchPopupController;
pageRef.getparameters().put('recordId',testque.id);
Test.setCurrentPage(pageRef);
ProductSearchPopupController.wrapProduct test=new
ProductSearchPopupController.wrapProduct(testpbe);
ApexPages.StandardController sc = new ApexPages.StandardController(testque);
ProductSearchPopupController psp = new ProductSearchPopupController(sc);
psp.wrapProductList.add(Test);
psp.selectedWrapperList.add(test);
psp.runQuery();
psp.runQuery1();
psp.ProceedWithSelectedToNextPage();
psp.processSelected();
psp.GoBack();
psp.saveproduct();
psp.callOnPageLoad();
}
}
ClAass
global class ProductSearchPopupController {
public String query {get; set;}
public List<PricebookEntry__c> products {get; set;}
public List<wrapProduct> wrapProductList {get; set;}
public List<QuoteLineitem__c> selectedProduct{get;set;}
public List<QuoteLineitem__c> quoteLineList{get;set;}
public List<wrapProduct> selectedWrapperList{get;set;}
public Boolean normalList{get;set;}
public Boolean selectedList{get;set;}
public Boolean block{get;set;}
public Boolean block1{get;set;}
public Boolean block2{get;set;}
public Boolean Error{get;set;}
public String SalesPrice {get; set;}
public integer Discount {get; set;}
public String Quantity {get; set;}
public String ServiceDate {get; set;}
public list<PricebookEntry__c> PricebookEntryList{get;set;}
public string selectePriceBook{get;set;}
public list<selectOption> listofpricebook{get;set;}
Id recordId;
public ProductSearchPopupController(ApexPages.StandardController controller){
recordId = controller.getId();
SalesPrice='';
Quantity='';
ServiceDate='';
system.debug('recordId '+recordId);
wrapProductList = new List<wrapProduct>();
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = true;
block = true;
block1 = true;
block2 = false;
selectePriceBook='None';
Error=False;
listofpricebook=new list<selectOption>();
PricebookEntryList= new List<PricebookEntry__c>();
list<pricebook2__c> przblist=new list<pricebook2__c>([select id, name, Is_Standard_Price_Book__c, IsActive__c from pricebook2__c Where IsActive__c =True Limit 999]);
listofpricebook.clear();
listofpricebook.add(new SelectOption('None','--None--'));
for(pricebook2__c pb: przblist)
{
listofpricebook.add(new selectOption(pb.id,pb.name));
}
}
public PageReference runQuery1(){
List<PricebookEntry__c> searchResults=[select id, PricebookEntry__c.Name, Pricebook2Id__r.Id, Pricebook2Id__r.Name, Product2Id__r.Product_Code__c, Product2Id__r.Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c from PricebookEntry__c where IsActive__c = true And Pricebook2Id__c=:selectePriceBook AND Product2Id__r.Name!=null];
if(searchResults.size() >= 0){
for(PricebookEntry__c a: searchResults) {
// As each product is processed we create a new wrapproduct object and add it to the wrapProductList
wrapProductList.add(new wrapProduct(a));
block = true;
block1 = true;
block2 = false;
}
}
return null;
}
public PageReference runQuery(){
wrapProductList = new List<wrapProduct>();
if(Test.isRunningTest()){
query='Test';
}
if(query == null || query == '' || query.length() ==1){
runQuery1();
}else if(query.length() >1){
query = '%'+query+'%';
List<PricebookEntry__c> searchResults=[Select id, Name,Product_Name__c,Product2Id__c, Pricebook2Id__r.Id, Pricebook2Id__r.Name, Product2Id__r.Name, Product2Id__r.Product_Code__c,Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c
from PricebookEntry__c where (Product2Id__r.Name like:query OR Product2Id__r.Product_Code__c like:query) And Pricebook2Id__c=:selectePriceBook Limit 1];
if(searchResults.size()>0){
// As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
wrapProductList.add(new wrapProduct(searchResults[0]));
block = true;
block1 = true;
block2 = false;
}
query = query.replaceAll('%','');
}
return null;
}
public PageReference ProceedWithSelectedToNextPage(){
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = true;
for(wrapProduct selectedWrapObj: wrapProductList){
system.debug('selectedWrapObj.selected ---------'+selectedWrapObj.selected);
if(selectedWrapObj.selected == true)
selectedWrapperList.add(selectedWrapObj);
}
system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
pageRef.setRedirect(false);
return pageRef;
}
public void processSelected() {
selectedProduct = new List<QuoteLineitem__c>();
for(wrapProduct wrapProductObj : wrapProductList) {
if(wrapProductObj.selected == true) {
QuoteLineitem__c qli = new QuoteLineitem__c();
qli.QuotesId__c = recordId;
qli.ListPrice__c = wrapProductObj.acc.UnitPrice__c;
qli.UnitPrice__c = wrapProductObj.acc.UnitPrice__c;
qli.Product2Id__c = wrapProductObj.acc.Product2Id__c;
qli.Quantity__c = 1;
selectedProduct.add(qli);
block = true;
block1 = false;
block2 = true;
}else{
Error=True;
}
}
if(Error==True && selectedProduct.isEmpty()){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one product to continue'));
}
}
public void callOnPageLoad() {
runQuery1();
processSelected();
}
public void GoBack() {
block = true;
block1 = true;
block2 = false;
}
global class wrapProduct{
public PricebookEntry__c acc {get;set;}
public Boolean selected {get;set;}
public wrapProduct(PricebookEntry__c p) {
this.acc = p;
this.selected = false;
}
}
public pagereference saveproduct(){
if(Test.isRunningTest()){
insertTestData();
}
if(!selectedProduct.isEmpty()){
System.debug('selectedProduct' + selectedProduct.size());
for(QuoteLineitem__c sp:selectedProduct){
if(sp.Quantity__c == null || sp.Quantity__c==0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Quantity should not be blank or Zero'));
return null;
}
}
if(selectedProduct.size() > 0){
insert selectedProduct;
System.debug('selectedProduct' + selectedProduct);
PageReference pageRef = new PageReference('/'+recordId);
pageRef.setRedirect(true);
return pageRef;
}
}
return null;
}
public void insertTestData(){
Product2__c testProduct = new Product2__c();
testProduct.Name='Test product';
insert testProduct;
PricebookEntry__c testpbe = new PricebookEntry__c();
testpbe.Name='Test PriceBookEntry';
testpbe.UnitPrice__c =123;
testpbe.Product2Id__c=testProduct.Id;
insert testpbe;
//selectedProduct.add(testpbe);
}
}
Test Class
@isTest
Private class TestProductSearchpopupController {
static testMethod void method() {
Account acc = new Account ();
acc.Name = 'Test1';
insert acc;
Opportunities__c opp= new Opportunities__c ();
opp.Name= 'Testopp1';
opp.Account_Name__c = acc.id;
opp.Close_Date__c = date.today();
opp.Stage__c = 'Prospecting';
insert opp;
Quotes__c testque = new Quotes__c();
testque.Name = 'Test PriceBookEntry';
testque.OpportunityId__c = opp.id;
testque.AccountId__c = acc.id;
insert testque;
List <Product2__c> lstproduct = new List<Product2__c>();
Product2__c testProduct = new Product2__c();
testProduct.Name='Test product';
lstproduct.add(testProduct);
insert lstproduct;
List <PricebookEntry__c> lstpbe = new List<PricebookEntry__c>();
PricebookEntry__c testpbe = new PricebookEntry__c();
testpbe.Name='Test';
lstpbe.add(testpbe);
insert lstpbe;
product2 pr = new product2();
pr.name = 'Test product';
insert pr;
List<Pricebook2> pricebook2List = new List<pricebook2>();
PriceBook2 pb2=new PriceBook2();
pb2.Name = 'test';
pb2.IsActive = true;
pricebook2List.add(pb2);
insert pricebook2List;
PricebookEntry two = new PricebookEntry();
two.pricebook2Id = Test.getStandardPricebookId();
two.product2id = pr.id;
two.unitprice = 1249.0;
two.isactive = true;
insert two;
PricebookEntry one = new PricebookEntry();
one.pricebook2Id = pb2.id;
one.product2id = pr.id;
one.unitprice = 1249.0;
one.isactive = true;
insert one;
List <QuoteLineitem__c> lstqli = new List<QuoteLineitem__c>();
QuoteLineitem__c testqli = new QuoteLineitem__c();
testqli.QuotesId__c = testque.id;
testqli.Quantity__c = 12;
lstqli.add(testqli);
insert lstqli;
PageReference pageRef = Page.ProductSearchPopupController;
pageRef.getparameters().put('recordId',testque.id);
Test.setCurrentPage(pageRef);
ProductSearchPopupController.wrapProduct test=new
ProductSearchPopupController.wrapProduct(testpbe);
ApexPages.StandardController sc = new ApexPages.StandardController(testque);
ProductSearchPopupController psp = new ProductSearchPopupController(sc);
psp.wrapProductList.add(Test);
psp.selectedWrapperList.add(test);
psp.runQuery();
psp.runQuery1();
psp.ProceedWithSelectedToNextPage();
psp.processSelected();
psp.GoBack();
psp.saveproduct();
psp.callOnPageLoad();
}
}
ClAass
global class ProductSearchPopupController {
public String query {get; set;}
public List<PricebookEntry__c> products {get; set;}
public List<wrapProduct> wrapProductList {get; set;}
public List<QuoteLineitem__c> selectedProduct{get;set;}
public List<QuoteLineitem__c> quoteLineList{get;set;}
public List<wrapProduct> selectedWrapperList{get;set;}
public Boolean normalList{get;set;}
public Boolean selectedList{get;set;}
public Boolean block{get;set;}
public Boolean block1{get;set;}
public Boolean block2{get;set;}
public Boolean Error{get;set;}
public String SalesPrice {get; set;}
public integer Discount {get; set;}
public String Quantity {get; set;}
public String ServiceDate {get; set;}
public list<PricebookEntry__c> PricebookEntryList{get;set;}
public string selectePriceBook{get;set;}
public list<selectOption> listofpricebook{get;set;}
Id recordId;
public ProductSearchPopupController(ApexPages.StandardController controller){
recordId = controller.getId();
SalesPrice='';
Quantity='';
ServiceDate='';
system.debug('recordId '+recordId);
wrapProductList = new List<wrapProduct>();
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = true;
block = true;
block1 = true;
block2 = false;
selectePriceBook='None';
Error=False;
listofpricebook=new list<selectOption>();
PricebookEntryList= new List<PricebookEntry__c>();
list<pricebook2__c> przblist=new list<pricebook2__c>([select id, name, Is_Standard_Price_Book__c, IsActive__c from pricebook2__c Where IsActive__c =True Limit 999]);
listofpricebook.clear();
listofpricebook.add(new SelectOption('None','--None--'));
for(pricebook2__c pb: przblist)
{
listofpricebook.add(new selectOption(pb.id,pb.name));
}
}
public PageReference runQuery1(){
List<PricebookEntry__c> searchResults=[select id, PricebookEntry__c.Name, Pricebook2Id__r.Id, Pricebook2Id__r.Name, Product2Id__r.Product_Code__c, Product2Id__r.Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c from PricebookEntry__c where IsActive__c = true And Pricebook2Id__c=:selectePriceBook AND Product2Id__r.Name!=null];
if(searchResults.size() >= 0){
for(PricebookEntry__c a: searchResults) {
// As each product is processed we create a new wrapproduct object and add it to the wrapProductList
wrapProductList.add(new wrapProduct(a));
block = true;
block1 = true;
block2 = false;
}
}
return null;
}
public PageReference runQuery(){
wrapProductList = new List<wrapProduct>();
if(Test.isRunningTest()){
query='Test';
}
if(query == null || query == '' || query.length() ==1){
runQuery1();
}else if(query.length() >1){
query = '%'+query+'%';
List<PricebookEntry__c> searchResults=[Select id, Name,Product_Name__c,Product2Id__c, Pricebook2Id__r.Id, Pricebook2Id__r.Name, Product2Id__r.Name, Product2Id__r.Product_Code__c,Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c
from PricebookEntry__c where (Product2Id__r.Name like:query OR Product2Id__r.Product_Code__c like:query) And Pricebook2Id__c=:selectePriceBook Limit 1];
if(searchResults.size()>0){
// As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
wrapProductList.add(new wrapProduct(searchResults[0]));
block = true;
block1 = true;
block2 = false;
}
query = query.replaceAll('%','');
}
return null;
}
public PageReference ProceedWithSelectedToNextPage(){
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = true;
for(wrapProduct selectedWrapObj: wrapProductList){
system.debug('selectedWrapObj.selected ---------'+selectedWrapObj.selected);
if(selectedWrapObj.selected == true)
selectedWrapperList.add(selectedWrapObj);
}
system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
pageRef.setRedirect(false);
return pageRef;
}
public void processSelected() {
selectedProduct = new List<QuoteLineitem__c>();
for(wrapProduct wrapProductObj : wrapProductList) {
if(wrapProductObj.selected == true) {
QuoteLineitem__c qli = new QuoteLineitem__c();
qli.QuotesId__c = recordId;
qli.ListPrice__c = wrapProductObj.acc.UnitPrice__c;
qli.UnitPrice__c = wrapProductObj.acc.UnitPrice__c;
qli.Product2Id__c = wrapProductObj.acc.Product2Id__c;
qli.Quantity__c = 1;
selectedProduct.add(qli);
block = true;
block1 = false;
block2 = true;
}else{
Error=True;
}
}
if(Error==True && selectedProduct.isEmpty()){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one product to continue'));
}
}
public void callOnPageLoad() {
runQuery1();
processSelected();
}
public void GoBack() {
block = true;
block1 = true;
block2 = false;
}
global class wrapProduct{
public PricebookEntry__c acc {get;set;}
public Boolean selected {get;set;}
public wrapProduct(PricebookEntry__c p) {
this.acc = p;
this.selected = false;
}
}
public pagereference saveproduct(){
if(Test.isRunningTest()){
insertTestData();
}
if(!selectedProduct.isEmpty()){
System.debug('selectedProduct' + selectedProduct.size());
for(QuoteLineitem__c sp:selectedProduct){
if(sp.Quantity__c == null || sp.Quantity__c==0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Quantity should not be blank or Zero'));
return null;
}
}
if(selectedProduct.size() > 0){
insert selectedProduct;
System.debug('selectedProduct' + selectedProduct);
PageReference pageRef = new PageReference('/'+recordId);
pageRef.setRedirect(true);
return pageRef;
}
}
return null;
}
public void insertTestData(){
Product2__c testProduct = new Product2__c();
testProduct.Name='Test product';
insert testProduct;
PricebookEntry__c testpbe = new PricebookEntry__c();
testpbe.Name='Test PriceBookEntry';
testpbe.UnitPrice__c =123;
testpbe.Product2Id__c=testProduct.Id;
insert testpbe;
//selectedProduct.add(testpbe);
}
}