+ Start a Discussion
rohitash yadav 8rohitash yadav 8 

increase code coverage of wrapper class

Hi,

I have wrapper class and test class. My code coverage is 38%. Can anyone help me to increase code coverage.

Apex class is 
public with sharing class OpportunityLineItemsNew {
    public List<Package__c> selectedPackages = new List<Package__c>();
    public Package__c prdcat{get;set;}
    public List<Package__c> checkedPackages{get;set;}
    List<packagewrapper1> packagesListNew = new List<packagewrapper1>();
    public List<packagewrapper1> packages {get;set;}
    public List<Package__c> filteredList = new List<Package__c>();
    String opprid = System.currentPageReference().getParameters().get('id');
    public set<Id> AccountSelectedSet;
    public OpportunityLineItemsNew(ApexPages.StandardSetController controller) {
        prdcat = new Package__c();
        AccountSelectedSet = new set<Id>();
        checkedPackages = new List<Package__c>();
        packagesListNew = new List<packagewrapper1>();
        packages = new List<packagewrapper1>();
        
        for(Line_Item__c chpack: [select Id, Name, Package__c,Product_Category__c,Activity__c,Cost__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c from Line_Item__c where Opportunity__c = :opprid]){
            Package__c exisOpprLineItems = new Package__c();
            exisOpprLineItems.Name = chpack.Name;
            exisOpprLineItems.Product_Category__c = chpack.Product_Category__c;
            exisOpprLineItems.Activity__c = chpack.Activity__c;
            exisOpprLineItems.Cost__c = chpack.Cost__c;
            exisOpprLineItems.Final_Price__c = chpack.Final_Price__c;
            exisOpprLineItems.No_of_Days__c = chpack.No_of_Days__c;
            exisOpprLineItems.No_Of_People__c = chpack.No_Of_People__c;
            exisOpprLineItems.No_of_Male__c = chpack.No_of_Male__c;
            exisOpprLineItems.No_of_Female__c = chpack.No_of_Female__c;
            exisOpprLineItems.Total_Price__c= chpack.Total_Price__c;
            exisOpprLineItems.Comments__c = chpack.Comments__c;
            checkedPackages.add(exisOpprLineItems);
            AccountSelectedSet.add(chpack.Package__c);
        }
    }

    public List<packagewrapper1> getPackages(){
        if( prdcat.Product_Category__c != null ){
            filteredList = [SELECT Id,Name,Product_Category__c,Cost__c,Stay__c,Activity__c,Description__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c FROM Package__c 
               where Product_Category__c=:prdcat.Product_Category__c ];
        } else {
            filteredList = [SELECT Id,Name,Product_Category__c,Cost__c,Stay__c,Activity__c,Description__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c FROM Package__c ];
        }
        for(Package__c a : filteredList ){
            if(AccountSelectedSet.contains(a.Id)) {
            } else {
                packages.add(new packagewrapper1(a));
            }
            packages.add(new packagewrapper1(a));
        }
        
        return packages;
    }
    
    public  PageReference  filterPackagess(){
        packages.clear();
        return null;
    }

    public Package__c getPrdcat() {
        prdcat = new Package__c();
        return null;
    }

    public void getSelectedPackages() {
        for(packagewrapper1 pkgwrapper : packages){
            if(pkgwrapper.selected == true){
                if(AccountSelectedSet.contains(pkgwrapper.pkgs.Id)) {
                } else {
                    AccountSelectedSet.add(pkgwrapper.pkgs.Id);
                    checkedPackages.add(pkgwrapper.pkgs);
                }
            }
        }
    }

    public PageReference cancel() {
        PageReference opportunityPage = new PageReference('/'+opprid);
        opportunityPage.setRedirect(true);
        return opportunityPage;
    }

    public PageReference save() {
        String opprid=System.currentPageReference().getParameters().get('id');
        for(Line_Item__c chpack: [select Id, Name, Package__c,Product_Category__c,Activity__c,Cost__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c from Line_Item__c where Opportunity__c = :opprid]){
            delete chpack;
        }
                
        for(Package__c chpack : checkedPackages){
            Line_Item__c oppline = new Line_Item__c();
            oppline.Name = chpack.Name;
            oppline.Product_Category__c= chpack.Product_Category__c;
            oppline.Activity__c = chpack.Activity__c;
            oppline.Cost__c = chpack.Cost__c;
            oppline.Final_Price__c = chpack.Final_Price__c;
            oppline.No_Of_People__c = chpack.No_Of_People__c;
            oppline.No_of_Male__c = chpack.No_of_Male__c;
            oppline.No_of_Female__c = chpack.No_of_Female__c;
            oppline.No_of_Days__c = chpack.No_of_Days__c;
            oppline.Total_Price__c= chpack.Total_Price__c;
            oppline.Comments__c = chpack.Comments__c;
            oppline.Opportunity__c = opprid;
            oppline.Package__c = chpack.Id;
            insert oppline;
        }

        PageReference opportunityPage = new PageReference('/'+opprid);
        opportunityPage .setRedirect(true);
        return opportunityPage ;
    }

    public class packagewrapper1 {
        public Package__c pkgs{get; set;}
        public Boolean selected {get; set;}
        public packagewrapper1(Package__c p) 
        { 
            pkgs = p;
            selected = false;
        }
    }
}

Test class is:
 
@isTest
private class Test_OpportunityLineItemsNew {
    static Opportunity oppor;
    static List<Line_Item__c> opporLineItem;
    static Payment_Schedule__c paySchedule;
    static List<Package__c> selectedPackages;
    static List<OpportunityLineItemsNew.packagewrapper1> packagesListNew;
    static List<OpportunityLineItemsNew.packagewrapper1> packages;
    static String opprid;

    static {
        packagesListNew = new List<OpportunityLineItemsNew.packagewrapper1>();
        packages = new List<OpportunityLineItemsNew.packagewrapper1>();
        
        oppor = new Opportunity();
        oppor.Name = 'Test Opportunity';
        oppor.Location__c = 'Nandi Hills';
        oppor.StageName = 'Lead';
        oppor.CloseDate = Date.newInstance(2016, 07, 22);
        insert oppor;
        
        opporLineItem = new List<Line_Item__c>(); 
        for(Package__c chpack: [select Id, Name, Product_Category__c,Activity__c,Cost__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c from Package__c where Product_Category__c = 'Full Day Package']){
            Line_Item__c oppline = new Line_Item__c();
            oppline.Name = chpack.Name;
            oppline.Product_Category__c= chpack.Product_Category__c;
            oppline.Activity__c = chpack.Activity__c;
            oppline.Cost__c = chpack.Cost__c;
            oppline.Final_Price__c = chpack.Final_Price__c;
            oppline.No_Of_People__c = 2;
            oppline.No_of_Male__c = 2;
            oppline.No_of_Female__c = 0;
            oppline.No_of_Days__c = 2;
            oppline.Total_Price__c= 2000;
            oppline.Comments__c = chpack.Comments__c;
            oppline.Opportunity__c = oppor.Id;
            oppline.Package__c = chpack.Id;
            opporLineItem.add(oppline);
        }

        insert opporLineItem;

        for(Line_Item__c chpack: [select Id, Name, Package__c,Product_Category__c,Activity__c,Cost__c,Final_Price__c,No_of_Days__c,No_Of_People__c,No_of_Male__c,No_of_Female__c,Total_Price__c,Comments__c from Line_Item__c where Opportunity__c = :oppor.Id]){
            
            ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
            stdSetController.setSelected(opporLineItem);
            OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);
            con.AccountSelectedSet.add(chpack.Package__c);
        }
    }

    static testMethod List<OpportunityLineItemsNew.packagewrapper1> Test_getPackages() {
        PageReference pref = Page.AddOpportunityLineItem2;
        pref.getParameters().put('id',oppor.Id);
        Test.setCurrentPage(pref);
        Test.startTest();

        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);
        
        Package__c pkgs = new Package__c();
        OpportunityLineItemsNew.packagewrapper1 op = new OpportunityLineItemsNew.packagewrapper1(pkgs);

        con.prdcat.Product_Category__c = null;
        con.packages = con.getPackages();
        
        Test.stopTest();
        return packages ;
    }
    
    static testMethod Void Test_getSelectedPackages() {
        PageReference pref = Page.AddOpportunityLineItem2;
        pref.getParameters().put('id',oppor.Id);
        Test.setCurrentPage(pref);
        Test.startTest();

        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);

        con.getSelectedPackages();

        Test.stopTest();

    }
    
    static testMethod PageReference Test_filterPackagess(){
        PageReference pref = Page.AddOpportunityLineItem2;
        Test.setCurrentPage(pref);
        Test.startTest();
        
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);

        pref = con.filterPackagess();

        Test.stopTest();
        return pref;
    }
    
    static testMethod PageReference Test_Save(){
        PageReference pref = Page.AddOpportunityLineItem2;
        Test.setCurrentPage(pref);
        Test.startTest();

        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);

        //getCheckecPackages
        pref = con.save();

        Test.stopTest();
        return pref;
    }
    
    static testMethod PageReference Test_Cancel(){
        PageReference pref = Page.AddOpportunityLineItem2;
        Test.setCurrentPage(pref);
        Test.startTest();

        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);
        pref = con.cancel();
        Test.stopTest();
        return pref;
    }

    static testMethod void TestgetSelectedPackages(){
        PageReference pref = Page.AddOpportunityLineItem2;
        Test.setCurrentPage(pref);
        Test.startTest();

        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(opporLineItem);
        stdSetController.setSelected(opporLineItem);
        OpportunityLineItemsNew con = new OpportunityLineItemsNew (stdSetController);

        con.getSelectedPackages();

        Test.stopTest();
    }
}

 
Best Answer chosen by rohitash yadav 8
VineetKumarVineetKumar
Well this is the only wrapper I see :
public class packagewrapper1 {
	public Package__c pkgs{get; set;}
	public Boolean selected {get; set;}
	public packagewrapper1(Package__c p){
		pkgs = p;
		selected = false;
	}
}
For using this in your code :
  1. Create a record for package object.
  2. Then instantiate your wrapper and pass the above created package object to the constructor.
packagewrapper1 pwObject = new packagewrapper1 (<packageRecord>);
  1. Set the flag variabe as true for the above :
    pwObject.selected = true;
    Instantiate your controller and assign the above created values to the variable 
    OpportunityLineItemsNew olin = new OpportunityLineItemsNew();
    olin.packages.add(pwObject);
    
    Call your save method in your test class.
    olin.save();

All Answers

VineetKumarVineetKumar
While creating setup data, create an object of your wrappper class and set the values to it's elements.
rohitash yadav 8rohitash yadav 8
Hi Vineet,

I have 2 custom objects packages__c and line_items__c and opportunity object. I have created a visualforce in which all packages are displaing. when I click on checkbox then selected package will display on another table and when click on save button then all selected packages will add to the opportunity. Can you please check my wrapper class and also explain the test class in detail.

Please provide some example also.

Thanks,
Rohitash
VineetKumarVineetKumar
Well this is the only wrapper I see :
public class packagewrapper1 {
	public Package__c pkgs{get; set;}
	public Boolean selected {get; set;}
	public packagewrapper1(Package__c p){
		pkgs = p;
		selected = false;
	}
}
For using this in your code :
  1. Create a record for package object.
  2. Then instantiate your wrapper and pass the above created package object to the constructor.
packagewrapper1 pwObject = new packagewrapper1 (<packageRecord>);
  1. Set the flag variabe as true for the above :
    pwObject.selected = true;
    Instantiate your controller and assign the above created values to the variable 
    OpportunityLineItemsNew olin = new OpportunityLineItemsNew();
    olin.packages.add(pwObject);
    
    Call your save method in your test class.
    olin.save();
This was selected as the best answer
rohitash yadav 8rohitash yadav 8
Hi Vineet,

Thanks for your help.

Now total code coverage is above 75% . I have done some changes in opportunity layout and created managed package and install it to another salesforce account. I want to set default page layout in opportunity to the page layout of opportunity in package. How it is possible.

Thanks,
Rohitash
VineetKumarVineetKumar
Check for page layout assigment on the Opportunity object page.
 
rohitash yadav 8rohitash yadav 8
How can it be done automatically when I install the package. 
VineetKumarVineetKumar
Are you not getting the required page layouts after the package installation?
rohitash yadav 8rohitash yadav 8
Yes
VineetKumarVineetKumar
Is it a managed or a unmanaged package?
rohitash yadav 8rohitash yadav 8
It is managed package and I have done some changes in default page layout of opportunity