function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bittu myanabittu myana 

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;
    }
    
}
 
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] [5] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

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
 
bittu myanabittu myana
Hi pcon,
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));
        }
    }
}
pconpcon
I have taken the liberty to reformat your code to make it easier to reference.  Can you please tell me what error you are seeing and what line number it coresponds to below?

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.
 
@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
            )); 
        }
    }
}
NOTE: I left your error on line 30 of inserting the empty list.  This should be on line 40 after your for loop
 
@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();
    }
}

 
bittu myanabittu myana

Controller:

/*/*class Name: OpportunityProductsFieldSetsCTRL
Version     : 39 
Created Date : 18 April 2017
Function    : Products dispaly using pagination and Field sets
*/
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 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;
    public boolean ProductCheck{get;set;}
    /*
* Method name  : getPaginationSizeOptions()
* Description  : selected options functionality
* Return Type  : List<SelectOption>
* Parameter    : 
*/
    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;
    }
    
    //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);
            ProductCheck=true;
        }
    } 
    //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));
                noOfRecords = setCon.getResultSize();
                OldQuery = query;
                if(size != null){
                    setCon.setPageSize(size);
                }
            }
            noOfRecords = setCon.getResultSize();   
            return setCon;
        }
        set;
    }
    
    /*
* Method name  : SearchProductDisplay()
* Description  : search the products according to the filter conditions
* Return Type  : void
* Parameter    : 
*/
    public void SearchProduct(){
        System.debug('Page size in fun'+size);
        if(setCon!=null){
            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;
        setCon = new ApexPages.StandardSetController(Database.getQueryLocator(Query));
        index=size;
        if(size!=null)
            setCon.setPageSize(size);
        
    }    
    /*
* Method name  : refreshPageSize()
* Description  : changes the page size according to the user requirement.
* Return Type  : PageReference 
* Parameter    : 
*/
    //Changes the size of pagination
    public PageReference refreshPageSize() {
        if(size != null){
            index = size;
            setCon.setPageSize(size);
        }
        return null;
    }
    /*
* Method name  : getProductList()
* Description  : to add list of products to product wrapper class
* Return Type  : list<ProductwrapClass>
* Parameter    : 
*/ 
    public list<ProductwrapClass> getProductList() {
        System.debug('start    ---');
        issampleListNull = false;
        ProductCheck=true;
        for (Product2 a : (list<Product2>)setCon.getRecords()) {
            ProductList.add(new ProductwrapClass(a,issampleListNull));
        }  
        issampleListNull = true; 
        if(ProductList.size()== 0 & ProductCheck==true) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'no products avilable'));
            ProductCheck=false;
        }
        
        return ProductList ;
    }
    /*
* Method name  : selectedProducts()
* Description  : Based on check box it will select the products 
* Return Type  : void
* Parameter    : 
*/
    list<Product2> listOfSelectedProducts=new list<Product2>();
    public void selectedProducts(){
        ProductCheck=true;
        for(ProductwrapClass p : ProductList ){
            if(p.aCheckBox == true) {
                
                selectedList.add(p);
                listOfSelectedProducts.add(p.aProduct);
                IsAdding =true;
            }
            else {
                ProductCheck=true;
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,' Please select at least one Product '));
            }
        } 
    }
    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
    
    /*
* Method name  : onCancel()
* Description  : cancel functionality it will redirected to opportunity page when we click on cancel button.
* Return Type  : PageReference
* Parameter    : 
*/ 
    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'));
    }
    /*
* Method name  : onSave()
* Description  : It willl save the selected products to opportunity line item.
* Return Type  : PageReference
* Parameter    : 
*/ 
    
    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);
        }
        if(opplinesToInsert.size()!=null){
            try{
                insert opplinesToInsert;
                if(Test.isRunningTest()){
                    Integer i=10/0;
                }
            }
            catch(Exception e){
                System.debug('ERROR:' + e);
            }
        }
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    } 
    
}

Wrapper class1:
/*class Name: OppLinItemWrapper
Version     : 39 
Created Date : 28 April 2017
Function    : Opportunity lineitem wrapper class for the class Products_Page_CTRL 
*/
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;
    }
}

Wrapper class2:

/*/*class Name: ProductwrapClass
Version     : 39 
Created Date : 28 April 2017
Function    : ProductwrapClass wrapper class for the class Products_Page_CTRL 
*/

public class ProductwrapClass {
    public Boolean aCheckBox {get;set;}
    public Product2 aProduct {get;set;}
    
    // sub-class initialization
    public ProductwrapClass(Product2 a, Boolean chk){
        aProduct = a;
        aCheckBox = chk;
        
    }
}



Utility class:
-------------------
@isTest
public class TestUtil {
    public static void createTestRecords(Integer numOpps, Integer nopricBokks,integer noproducts) {
        List<opportunity> oppsList = new List<opportunity>();

     
            opportunity o = new opportunity(Name='TestAccount',StageName = 'Prospecting',CloseDate = System.today());  
            
        insert o;
         
        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
            )); 
        }
    }
}

Test Class:
--------------
@isTest
private class MyTestClass {
    static testmethod void test1() {
        TestUtil.createTestRecords(1,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>();
        
        opportunity o=new opportunity();
        
        test.startTest();
        ApexPages.StandardController stdSetController = new ApexPages.StandardController(o);
        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();
    }
}

 
bittu myanabittu myana
Hi pcon,
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
 
pconpcon
Did you move the insert in your PricebookEntry list after you populated it?  I still recommend that you rewrite your test to actually test individual components of your class.  I would create a test method for each method inside of your controller and test them individually.  Additionally, adding asserts to your tests would let you know why your test is not getting 100% coverage.  For example, your getOppLinItemWrapper method returns a list of OppLinItemWrapper.  You should be iterating over the results of that and verifying that you get back the number and value of them that you expect.  I would expect that you do not.  This is most likely because your test data is not generated properly.