You need to sign in to do that
Don't have an account?
Test Methods only covering 50% for below test class
How do i cover my apex code for 100%
My test class with 50% code coverage
@isTest(SeeAllData=true)
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 PriceBookEntry';
lstpbe.add(testpbe);
insert lstpbe;
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);
PageReference pageRef = Page.ProductSearchPopupController;
pageRef.getparameters().put('recordId',testque.id);
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(testque);
ProductSearchPopupController psp = new ProductSearchPopupController(sc);
psp.runQuery();
psp.ProceedWithSelectedToNextPage();
psp.processSelected();
psp.GoBack();
psp.saveproduct();
}
}
My class - my Apex class code where red lines are not getting covered
public class ProductSearchPopupController {
public String query {get; set;}
public List<PricebookEntry__c> products {get; set;}
public List<wrapProduct> wrapProductList {get; set;}
public List<PricebookEntry__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 String SalesPrice {get; set;}
public integer Discount {get; set;}
public String Quantity {get; set;}
public String ServiceDate {get; set;}
Id recordId;
public ProductSearchPopupController(ApexPages.StandardController controller){
recordId = controller.getId();
system.debug('recordId '+recordId);
wrapProductList = new List<wrapProduct>();
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = false;
block = true;
block1 = false;
block2 = false;
}
public PageReference runQuery(){
if(query == null || query == ''){
system.debug('query '+query);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Please enter the product to be searched'));
return null;
}
List<List<PricebookEntry__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING PricebookEntry__c (id, Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c)];
system.debug('searchResults '+searchResults.size()+searchResults[0].size()+searchResults.isempty());
if(searchResults[0]!=null){
for(PricebookEntry__c a: searchResults[0]) {
// As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
wrapProductList.add(new wrapProduct(a));
block = true;
block1 = true;
block2 = false;
}
}
return null;
}
public PageReference ProceedWithSelectedToNextPage(){
selectedWrapperList = new List<wrapProduct>();
normalList = false;
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<PricebookEntry__c>();
for(wrapProduct wrapProductObj : wrapProductList) {
if(wrapProductObj.selected == true) {
selectedProduct.add(wrapProductObj.acc);
block = false;
block1 = false;
block2 = true;
}
}
}
public void GoBack() {
block = true;
block1 = true;
block2 = false;
}
public 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(){
List<QuoteLineitem__c> quoteLineList = new List<QuoteLineitem__c>();
if(!selectedProduct.isEmpty()){
for(PricebookEntry__c sp:selectedProduct){
system.debug('sp '+sp);
QuoteLineitem__c qli = new QuoteLineitem__c();
qli.QuotesId__c = recordId;
qli.ListPrice__c = sp.UnitPrice__c;
qli.UnitPrice__c = sp.UnitPrice__c;
qli.Product2Id__c = sp.Product2Id__c;
if(Discount!=0 || Discount!=null){
qli.Discount__c = Discount;
}
quoteLineList.add(qli);
}
if(quoteLineList.size()>0){
insert quoteLineList;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+recordId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
}
return null;
}
}
My test class with 50% code coverage
@isTest(SeeAllData=true)
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 PriceBookEntry';
lstpbe.add(testpbe);
insert lstpbe;
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);
PageReference pageRef = Page.ProductSearchPopupController;
pageRef.getparameters().put('recordId',testque.id);
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(testque);
ProductSearchPopupController psp = new ProductSearchPopupController(sc);
psp.runQuery();
psp.ProceedWithSelectedToNextPage();
psp.processSelected();
psp.GoBack();
psp.saveproduct();
}
}
My class - my Apex class code where red lines are not getting covered
public class ProductSearchPopupController {
public String query {get; set;}
public List<PricebookEntry__c> products {get; set;}
public List<wrapProduct> wrapProductList {get; set;}
public List<PricebookEntry__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 String SalesPrice {get; set;}
public integer Discount {get; set;}
public String Quantity {get; set;}
public String ServiceDate {get; set;}
Id recordId;
public ProductSearchPopupController(ApexPages.StandardController controller){
recordId = controller.getId();
system.debug('recordId '+recordId);
wrapProductList = new List<wrapProduct>();
selectedWrapperList = new List<wrapProduct>();
normalList = true;
selectedList = false;
block = true;
block1 = false;
block2 = false;
}
public PageReference runQuery(){
if(query == null || query == ''){
system.debug('query '+query);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Please enter the product to be searched'));
return null;
}
List<List<PricebookEntry__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING PricebookEntry__c (id, Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c)];
system.debug('searchResults '+searchResults.size()+searchResults[0].size()+searchResults.isempty());
if(searchResults[0]!=null){
for(PricebookEntry__c a: searchResults[0]) {
// As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
wrapProductList.add(new wrapProduct(a));
block = true;
block1 = true;
block2 = false;
}
}
return null;
}
public PageReference ProceedWithSelectedToNextPage(){
selectedWrapperList = new List<wrapProduct>();
normalList = false;
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<PricebookEntry__c>();
for(wrapProduct wrapProductObj : wrapProductList) {
if(wrapProductObj.selected == true) {
selectedProduct.add(wrapProductObj.acc);
block = false;
block1 = false;
block2 = true;
}
}
}
public void GoBack() {
block = true;
block1 = true;
block2 = false;
}
public 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(){
List<QuoteLineitem__c> quoteLineList = new List<QuoteLineitem__c>();
if(!selectedProduct.isEmpty()){
for(PricebookEntry__c sp:selectedProduct){
system.debug('sp '+sp);
QuoteLineitem__c qli = new QuoteLineitem__c();
qli.QuotesId__c = recordId;
qli.ListPrice__c = sp.UnitPrice__c;
qli.UnitPrice__c = sp.UnitPrice__c;
qli.Product2Id__c = sp.Product2Id__c;
if(Discount!=0 || Discount!=null){
qli.Discount__c = Discount;
}
quoteLineList.add(qli);
}
if(quoteLineList.size()>0){
insert quoteLineList;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+recordId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
}
return null;
}
}