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
sales4cesales4ce 

How to write unit tests to cover wrapper/inner class

Hi,

 

I am unable to figure out how i can write test methods to cover code for my wrapper/inner class.

Can any one point me in the right direction?

 

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

That wouldn't quite work as getDtWrap returns a list of DtWrapper rather than a single instance.

 

Testing wrapper classes is no different to testing controllers, but you usually have to put a bit more effort in to set up the data correctly, as you are usually encapsulating lots of different information.

 

In this case, once you've instantiated the controller you have added an empty instance of a dtwrapper to the list, so you should be able to pull that element back and execute the methods that will give you the coverage.

 

Something like the following:

 

DtWrapper candWrap=controller.getDtWrap()[0];

List<SelectOption> listGroups=candWrap.getListGroups();

System.assert(listGroups.size()>0);
System.assertEquals(candWrap.getListProducts().size(), 0);

// to get full coverage it looks like you need to set currentGroup, but I don't know what your values are so have made them up candWrap.currentGroup='<groupid>'; List<SelectOption> listProds=candWrap.getListProducts(); // assert something here regarding list products System.assertEquals(candWrap.getListTUCs().size(), 0);

// to get full coverage regarding TUCs you should set the currentProduct candWrap.currentProduct='<prodid>'; List<SelectOption> listTUCs=candWrap.getListTUCs(); // assert something here candWrap.currentTUC='<tucid>'; // etc

 

 

 

All Answers

MiddhaMiddha

You dont need to write any separate test classes for your wrapper classes. If you have written your tests well, covering most of the use cases, the wrapper classes should be called from withing your test classes. If wrapper classes are not covered, this means that you are missing some important use case and should add some more test cases.

sales4cesales4ce

Thanks Gulshan. I am unable to get coverage for this part of the code in "Red". Can you point me in the right direction.

 

Apex class:

 

public class DDSPriceSupport {

    Public Pricing_Support__c priceSupport;
    Public DDS_Visit__c visit;
    Public List<Pricing_Support_Products__c> prodList;
    List<dtWrapper> dtwrap=New List<dtWrapper>();
// Controller
    public DDSPriceSupport(ApexPages.StandardController controller) {
         visit= (DDS_Visit__c) controller.getRecord();
         dtwrap.add(new dtWrapper());
         prodList=New List<Pricing_Support_Products__c>();

    }
       
    Public pageReference actionSave(){
        
        priceSupport.Name='Price Support Requested for:'+Visit.Name;
        priceSupport.DDS_Visit__c=visit.Id;
        
        Insert priceSupport;
        
        for( Integer i=0;i<dtwrap.size();i++){
            Pricing_Support_Products__c newProd=New Pricing_Support_Products__c();
            newProd.Name=priceSupport.Name;
             newProd.Product_Classification__c=dtwrap[i].currentGroup;
            newProd.Pricing_Support__c=priceSupport.Id;
            newProd.Product_Description__c=dtwrap[i].getTucDescribe();
            newProd.Projected_Volume_Eaches__c=dtwrap[i].projectedVolume;
            newProd.Price_Requested_Eaches__c=dtwrap[i].priceRequested;
            newProd.List_Tuc__c=dtwrap[i].getTucName();
            System.Debug(' Product Group Selected >>>'+dtwrap[i].currentGroup);
            prodList.add(newProd);
        }
        
        Insert prodList;
        priceSupport.Send_Email__c=true;
        update priceSupport;
       System.Debug('DDS Visit Record Id >>>>'+visit.Id);
        System.Debug('Inserted Pricing Support Record >>>>>'+priceSupport.Id);
        System.Debug('Inserted Pricing Support Record Related to DDS Visit >>>>>'+priceSupport.DDS_Visit__c);
        PageReference pg=New PageReference('/apex/DDSVisitDetail');
         pg.getParameters().Put('Id', visit.Id);
        return pg;
    }
    
    Public List<dtWrapper> getdtwrap(){
        return dtwrap;
    }
    Public void setdtwrap(List<dtWrapper> wrapit){
        this.dtwrap=wrapit;
    }
    
    Public PageReference addProduct(){
        dtwrap.add(New dtWrapper());
        return Null;
    }
    Public Pricing_Support__c getPriceSupport(){
        if(priceSupport==Null)
            priceSupport=New Pricing_Support__c();
        return priceSupport;
    }
    
     Public Class dtWrapper{
    
        Public String currentGroup {get;set;}
        Public String currentProduct {get; set;}
        Public String currentTuc {get;set;}
        Public Integer projectedVolume ;
        Public Double priceRequested ;
        Public Double totalPrice ;
        
        Public dtWrapper(){
            currentGroup=currentProduct=currentTuc=Null;
            projectedVolume=0;
            priceRequested=totalPrice=0.0;
            
        }
        
        Public Integer getprojectedVolume(){
            return projectedVolume;
        }
        
        Public void setprojectedVolume(Integer vol){
            projectedVolume=vol;
            
        }
        
        Public Double getpriceRequested(){
            return priceRequested;
        }
        
        Public Void setpriceRequested(double pri){
            priceRequested=pri;
            
        }
        
        Public Double gettotalPrice(){
            return totalPrice;
        }
        
        Public pageReference calculateTotalValue(){
            totalPrice=(projectedVolume*priceRequested);
            return Null;
        }
        
        public List<SelectOption> getListGroups() {
        List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
        for(Schema.PicklistEntry pe:Product2.Family.getDescribe().getPicklistValues()) {
              if(pe.getValue()=='Drugs'){
                options.add(new SelectOption(pe.getValue(),pe.getLabel()));
              }
        }
        return options;
    }
    
    public List<SelectOption> getListProducts() {
        List<SelectOption> options = new List<SelectOption>();
        if(currentGroup == null || currentGroup == '')
            return options;
        options.add(new SelectOption('','-- Choose --'));
        for(Product2 p2:[select id,name from product2 where family = :currentGroup]) {
            options.add(new SelectOption(p2.id,p2.name));
        }
        return options;
    }
    
    public List<SelectOption> getListTUCs() {
        List<SelectOption> options = new List<SelectOption>();
        if(currentProduct==null || currentProduct == '')
            return options;
        options.add(new SelectOption('','-- Choose --'));
        for(List_Tuc__c tuc:[select id,name from List_Tuc__c where product__c = :currentProduct]) {
            options.add(new SelectOption(tuc.Id,tuc.name));
        }
        return options;
    }

    public String getTucDescribe() {
        if(currentTuc == null || currentTuc == '')
            return '';
        return [select Name from List_Tuc__c where id = :currentTuc].Name;
    }
    
    public String getTucName(){
    
        if(currentTuc == null || currentTuc == '')
            return '';
        return [select  List_Tuc__c from List_Tuc__c where id = :currentTuc].List_Tuc__c;
    }
        
    
    }


}




Test Class

/**
 * This class contains unit tests for validating the behavior of DDSPriceSupport Apex Class.
 
 */
@isTest
private class testDDSPriceSupport {

    static testMethod void myUnitTest() {
        
        //Create a test Account
        
        Account testAcct=New Account(Name='Test Account',Billingcity='Lake Forest',BillingState='IL');
        Insert testAcct;
        
        //Create a Test Visit
        
        DDS_Visit__c visit=New DDS_Visit__c(Account__c=testAcct.Id,Visit_Date__c = Date.today().addDays(30),Visit_Stage__c = 'Planning');
        Insert visit;
        
        // Instantiate the controller
        
        Test.StartTest();
        DDSPriceSupport controller=new DDSPriceSupport(
                                       new ApexPages.StandardController(visit));

        
        controller.getdtwrap();
        controller.addProduct();
        controller.getPriceSupport();
        controller.actionSave();
        
        
        Test.stopTest();                                    
        
        
    }
}



Jake GmerekJake Gmerek

I have never actually done it, but in theory you should be able to do:

 

controller.getdtwrap().getListGroups();

 

I would think that that would work and call the methods that you need.

bob_buzzardbob_buzzard

That wouldn't quite work as getDtWrap returns a list of DtWrapper rather than a single instance.

 

Testing wrapper classes is no different to testing controllers, but you usually have to put a bit more effort in to set up the data correctly, as you are usually encapsulating lots of different information.

 

In this case, once you've instantiated the controller you have added an empty instance of a dtwrapper to the list, so you should be able to pull that element back and execute the methods that will give you the coverage.

 

Something like the following:

 

DtWrapper candWrap=controller.getDtWrap()[0];

List<SelectOption> listGroups=candWrap.getListGroups();

System.assert(listGroups.size()>0);
System.assertEquals(candWrap.getListProducts().size(), 0);

// to get full coverage it looks like you need to set currentGroup, but I don't know what your values are so have made them up candWrap.currentGroup='<groupid>'; List<SelectOption> listProds=candWrap.getListProducts(); // assert something here regarding list products System.assertEquals(candWrap.getListTUCs().size(), 0);

// to get full coverage regarding TUCs you should set the currentProduct candWrap.currentProduct='<prodid>'; List<SelectOption> listTUCs=candWrap.getListTUCs(); // assert something here candWrap.currentTUC='<tucid>'; // etc

 

 

 

This was selected as the best answer
sales4cesales4ce

Bob,

 

Thanks for your response. But this part of the code gives me an error in my test code.

 

DtWrapper candWrap=controller.getDtWrap()[0];

 

error message: invalid type DtWrapper.

 

Any pointers?

 

Thanks,

Sales4ce

bob_buzzardbob_buzzard

My bad.  I'm treating DtWrapper as a top level class.

 

Should be:

 

DDSPriceSupport.DtWrapper candWrap=controller.getDtWrap()[0];