You need to sign in to do that
Don't have an account?
bittu myana
public class OpportunityProductsFieldSetsCTRL {
Public Integer size{get;set;}
public product2 productSearch {set;get;}
public List<OppLinItemWrapper> OppLinItemWrapper;
public OpportunityLineItem lineItems{set;get;}
Public Integer index{get;set;}
public list<ProductwrapClass> ProductList ;
public boolean issampleListNull {get;set;}
Public Integer noOfRecords{get; set;}
//public String SearchProductCode{get;set;}
public Product2 product {get;set;}
public list<ProductwrapClass> selectedList{get; set;}
public string Query;
Public string OldQuery;
public boolean IsAdding {get;set;}
public list<OppLinItemWrapper> OpplineList{get;set;}
public string opportunityId;
//String whereClause;
public List<SelectOption> getPaginationSizeOptions()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('10','10'));
options.add(new SelectOption('25','25'));
options.add(new SelectOption('50','50'));
options.add(new SelectOption('100','100'));
return options;
}
//string opportunityId;
//Constarctor
public OpportunityProductsFieldSetsCTRL(ApexPages.StandardController controller){
productSearch=new product2();
lineItems = new OpportunityLineItem();
IsAdding=false;
opportunityId=ApexPages.currentPage().getParameters().get('id');
product = new Product2();
opportunityId=ApexPages.currentPage().getParameters().get('id');
ProductList = new list<ProductwrapClass>();
selectedList= new list<ProductwrapClass>();
if(index == null){
index = 10;
setCon.setPageSize(10);
}
}
//Standard set controller
public ApexPages.StandardSetController setCon {
get {
ProductList.clear();
if(setCon == null||OldQuery != Query) {
if(Query == null || Query == '') {
Query = 'select id';
for(Schema.FieldSetMember fld :SObjectType.Product2.FieldSets.Product_Field_Set1.getFields()) {
Query += ', ' + fld.getFieldPath();
}
query += ' FROM Product2 ';
}
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
//System.debug('Query'+Query);
noOfRecords = setCon.getResultSize();
OldQuery = query;
if(size != null){
setCon.setPageSize(size);
}
}
noOfRecords = setCon.getResultSize();
System.debug('setCon.getResultSize()'+setCon.getResultSize());
return setCon;
}
set;
}
public void SearchProduct(){
System.debug('Page size in fun'+size);
Query = 'select id';
for(Schema.FieldSetMember fld :SObjectType.Product2.FieldSets.Product_Field_Set1.getFields()) {
Query += ', ' + fld.getFieldPath();
}
query=query+' from Product2';
String whereClause=' where IsActive=true';
for(Schema.FieldSetMember fldset :SObjectType.Product2.FieldSets.Search_Field_Set.getFields())
{
if((String)productSearch.get(fldset.getFieldPath())!=null)
{
whereClause=whereclause+' and '+fldset.getFieldPath()+' Like \'%'+(string)productSearch.get(fldset.getFieldPath())+'%\' ';
}
}
query=query+whereClause;
System.debug('%%%%'+query);
//system.debug('query --'+query);
//setCon=Database.getQueryLocator(query);
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
//System.debug('setCon-------'+setCon.getResultSize());
index=size;
if(size!=null)
setCon.setPageSize(size);
}
public PageReference refreshPageSize() {
if(size != null){
index = size;
setCon.setPageSize(size);
}
return null;
}
public list<ProductwrapClass> getProductList() {
System.debug('start ---');
issampleListNull = false;
for (Product2 a : (list<Product2>)setCon.getRecords()) {
ProductList.add(new ProductwrapClass(a,issampleListNull));
}
if(ProductList.size() == 0) {
issampleListNull = true;
}
System.debug('ProductList '+ProductList.size());
return ProductList ;
}
list<Product2> listOfSelectedProducts=new list<Product2>();
public void selectedProducts(){
for(ProductwrapClass p : ProductList ){
if(p.aCheckBox == true) {
selectedList.add(p);
listOfSelectedProducts.add(p.aProduct);
IsAdding =true;
}
}
}
list<OppLinItemWrapper> listOfLineItems=new list<OppLinItemWrapper>();
public List<OppLinItemWrapper> getOppLinItemWrapper (){
OpplineList=new list<OppLinItemWrapper>();
listOfLineItems.clear();
for(Product2 p1:listOfSelectedProducts){
OpportunityLineItem opportunityLine=new OpportunityLineItem();
PricebookEntry pbEntry = [select id,UnitPrice from PricebookEntry where Product2Id=:p1.Id limit 1];
opportunityLine.PricebookEntryId=pbEntry.Id;
opportunityLine.UnitPrice=pbEntry.UnitPrice;
opportunityLine.OpportunityId=opportunityId;
listOfLineItems.add(new OppLinItemWrapper(p1,opportunityLine));
}
return listOfLineItems;
}
//Wrapper Class
public class ProductwrapClass {
public Boolean aCheckBox {get;set;}
public Product2 aProduct {get;set;}
public ProductwrapClass(Product2 a, Boolean chk){
aProduct = a;
aCheckBox = chk;
}
}
public PageReference onCancel(){
// If user hits cancel we commit no changes and return them to the Opportunity
return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
}
/*
public PageReference onSave(){
list<OpportunityLineItem> opplinesToInsert=new List<OpportunityLineItem>();
System.debug('Saveeeeeeee');
opplinesToInsert.clear();
System.debug('listOfLineItems--------'+listOfLineItems.size());
for(OppLinItemWrapper lineItemWrap:listOfLineItems){
if(lineItemWrap.oppLineItem.Quantity!=null)
opplinesToInsert.add(lineItemWrap.oppLineItem);
system.debug('lineItemWrap::'+lineItemWrap.oppLineItem);
}
if(opplinesToInsert.size()!=null){
try{
insert opplinesToInsert;
}
catch(Exception e){
System.debug('ERROR:' + e);
}
}
return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
}
}
public class OppLinItemWrapper {
public product2 productVar{get;set;}
public OpportunityLineItem oppLineItem{get;set;}
// sub-class initialization
public OppLinItemWrapper(product2 p,OpportunityLineItem o){
productVar=p;
oppLineItem=o;
}
}
How to write test class with asserts for the below code
public class OpportunityProductsFieldSetsCTRL {
Public Integer size{get;set;}
public product2 productSearch {set;get;}
public List<OppLinItemWrapper> OppLinItemWrapper;
public OpportunityLineItem lineItems{set;get;}
Public Integer index{get;set;}
public list<ProductwrapClass> ProductList ;
public boolean issampleListNull {get;set;}
Public Integer noOfRecords{get; set;}
//public String SearchProductCode{get;set;}
public Product2 product {get;set;}
public list<ProductwrapClass> selectedList{get; set;}
public string Query;
Public string OldQuery;
public boolean IsAdding {get;set;}
public list<OppLinItemWrapper> OpplineList{get;set;}
public string opportunityId;
//String whereClause;
public List<SelectOption> getPaginationSizeOptions()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('10','10'));
options.add(new SelectOption('25','25'));
options.add(new SelectOption('50','50'));
options.add(new SelectOption('100','100'));
return options;
}
//string opportunityId;
//Constarctor
public OpportunityProductsFieldSetsCTRL(ApexPages.StandardController controller){
productSearch=new product2();
lineItems = new OpportunityLineItem();
IsAdding=false;
opportunityId=ApexPages.currentPage().getParameters().get('id');
product = new Product2();
opportunityId=ApexPages.currentPage().getParameters().get('id');
ProductList = new list<ProductwrapClass>();
selectedList= new list<ProductwrapClass>();
if(index == null){
index = 10;
setCon.setPageSize(10);
}
}
//Standard set controller
public ApexPages.StandardSetController setCon {
get {
ProductList.clear();
if(setCon == null||OldQuery != Query) {
if(Query == null || Query == '') {
Query = 'select id';
for(Schema.FieldSetMember fld :SObjectType.Product2.FieldSets.Product_Field_Set1.getFields()) {
Query += ', ' + fld.getFieldPath();
}
query += ' FROM Product2 ';
}
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
//System.debug('Query'+Query);
noOfRecords = setCon.getResultSize();
OldQuery = query;
if(size != null){
setCon.setPageSize(size);
}
}
noOfRecords = setCon.getResultSize();
System.debug('setCon.getResultSize()'+setCon.getResultSize());
return setCon;
}
set;
}
public void SearchProduct(){
System.debug('Page size in fun'+size);
Query = 'select id';
for(Schema.FieldSetMember fld :SObjectType.Product2.FieldSets.Product_Field_Set1.getFields()) {
Query += ', ' + fld.getFieldPath();
}
query=query+' from Product2';
String whereClause=' where IsActive=true';
for(Schema.FieldSetMember fldset :SObjectType.Product2.FieldSets.Search_Field_Set.getFields())
{
if((String)productSearch.get(fldset.getFieldPath())!=null)
{
whereClause=whereclause+' and '+fldset.getFieldPath()+' Like \'%'+(string)productSearch.get(fldset.getFieldPath())+'%\' ';
}
}
query=query+whereClause;
System.debug('%%%%'+query);
//system.debug('query --'+query);
//setCon=Database.getQueryLocator(query);
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
//System.debug('setCon-------'+setCon.getResultSize());
index=size;
if(size!=null)
setCon.setPageSize(size);
}
public PageReference refreshPageSize() {
if(size != null){
index = size;
setCon.setPageSize(size);
}
return null;
}
public list<ProductwrapClass> getProductList() {
System.debug('start ---');
issampleListNull = false;
for (Product2 a : (list<Product2>)setCon.getRecords()) {
ProductList.add(new ProductwrapClass(a,issampleListNull));
}
if(ProductList.size() == 0) {
issampleListNull = true;
}
System.debug('ProductList '+ProductList.size());
return ProductList ;
}
list<Product2> listOfSelectedProducts=new list<Product2>();
public void selectedProducts(){
for(ProductwrapClass p : ProductList ){
if(p.aCheckBox == true) {
selectedList.add(p);
listOfSelectedProducts.add(p.aProduct);
IsAdding =true;
}
}
}
list<OppLinItemWrapper> listOfLineItems=new list<OppLinItemWrapper>();
public List<OppLinItemWrapper> getOppLinItemWrapper (){
OpplineList=new list<OppLinItemWrapper>();
listOfLineItems.clear();
for(Product2 p1:listOfSelectedProducts){
OpportunityLineItem opportunityLine=new OpportunityLineItem();
PricebookEntry pbEntry = [select id,UnitPrice from PricebookEntry where Product2Id=:p1.Id limit 1];
opportunityLine.PricebookEntryId=pbEntry.Id;
opportunityLine.UnitPrice=pbEntry.UnitPrice;
opportunityLine.OpportunityId=opportunityId;
listOfLineItems.add(new OppLinItemWrapper(p1,opportunityLine));
}
return listOfLineItems;
}
//Wrapper Class
public class ProductwrapClass {
public Boolean aCheckBox {get;set;}
public Product2 aProduct {get;set;}
public ProductwrapClass(Product2 a, Boolean chk){
aProduct = a;
aCheckBox = chk;
}
}
public PageReference onCancel(){
// If user hits cancel we commit no changes and return them to the Opportunity
return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
}
/*
public PageReference onSave(){
list<OpportunityLineItem> opplinesToInsert=new List<OpportunityLineItem>();
System.debug('Saveeeeeeee');
opplinesToInsert.clear();
System.debug('listOfLineItems--------'+listOfLineItems.size());
for(OppLinItemWrapper lineItemWrap:listOfLineItems){
if(lineItemWrap.oppLineItem.Quantity!=null)
opplinesToInsert.add(lineItemWrap.oppLineItem);
system.debug('lineItemWrap::'+lineItemWrap.oppLineItem);
}
if(opplinesToInsert.size()!=null){
try{
insert opplinesToInsert;
}
catch(Exception e){
System.debug('ERROR:' + e);
}
}
return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
}
}
public class OppLinItemWrapper {
public product2 productVar{get;set;}
public OpportunityLineItem oppLineItem{get;set;}
// sub-class initialization
public OppLinItemWrapper(product2 p,OpportunityLineItem o){
productVar=p;
oppLineItem=o;
}
}
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
[5] https://developer.salesforce.com/trailhead/module/apex_testing
I have writen test class i have tried utility class for test class its gives an error,
inorder to insert the test data i have used sample utility class can you anyone help me to solve the problem.im have mentione dcode below
for the above class
@isTest
private class MyTestClass {
static testmethod void test1() {
TestUtil.createTestRecords(10,20,10);
List<opportunity> oppsList = new List<opportunity>();
List<Pricebook2> pricebkList = new List<Pricebook2>();
List<Product2> prductList = new List<Product2>();
List<PricebookEntry> pbEntrylist = new List<PricebookEntry>();
list<ProductwrapClass> notSelectedList=new list<ProductwrapClass>();
test.startTest();
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(oppsList);
OpportunityProductsFieldSetsCTRL controller=new OpportunityProductsFieldSetsCTRL(stdSetController);
controller.size=10;
controller.getPaginationSizeOptions();
controller.ProductList=notSelectedList;
controller.getProductList();
controller.SearchProduct();
list<ProductwrapClass> SelectedList=new list<ProductwrapClass>();
for(Product2 product:prductList)
{
ProductwrapClass prdWrps=new ProductwrapClass(product,true);
SelectedList.add(prdWrps);
}
controller.ProductList=SelectedList;
controller.selectedProducts();
controller.getOppLinItemWrapper();
controller.refreshPageSize();
controller.onSave();
controller.onCancel();
test.stopTest();
}
}
Utility Class:
@isTest
public class TestUtil {
public static void createTestRecords(Integer numOpps, Integer nopricBokks,integer noproducts) {
List<opportunity> oppsList = new List<opportunity>();
for(Integer i=0;i<numOpps;i++) {
opportunity o = new opportunity(Name='TestAccount' ,StageName = 'Prospecting', CloseDate = System.today());
oppsList.add(o);
}
insert oppsList;
List<Product2> prductList = new List<Product2>();
for(Integer i=0;i<nopricBokks;i++) {
Product2 prd = new Product2(Name='TestAccount' + i,isActive=true);
prductList.add(prd);
}
insert prductList;
List<PricebookEntry> pbEntrylist = new List<PricebookEntry>();
insert pbEntrylist;
for(Product2 product:prductList)
{
pbEntrylist.add(new PricebookEntry(unitprice=100,Product2Id=product.Id,Pricebook2Id=Test.getStandardPricebookId(),isActive=true,UseStandardPrice = false));
}
}
}
I also very very very strongly urge you to read over the links provided. The test class that you have provided (while possibly will meet the code coverage requirements) is not really a test. You are not testing a finite portion of your class, nor do you have any asserts to show that what you expect to happen is actually happening.
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
NOTE: I left your error on line 30 of inserting the empty list. This should be on line 40 after your for loop
I have tried in the above manner i got 85% code coverage with the help of utility class but opportunityLineItem wrapper is not covering i am looking for 1005 code coverage