+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Test Class for Wrapper Class having Search Functionality

Hi Team,
I have written a wrapper class contaning the search functionality. But I can't understand how to write it's test class.

APEX CODE 

Public class fetchserials{
    
    Public string OrderID {get; set;}
    Public order OrderInstance {get; set;}
    Public List<OrderItem> OrderItemList {get; set;}
    Public Set<id> ProductIdSet {get; set;} //list to add product Ids
    Public list<Serial_Number__c> SerialList {get; set;}
    Public list<Serial_Number__c> SelectedSerialList {get; set;}
    Public Map<OrderItem,list<Serial_Number__c>> OrderVSerial{get; set;} // list to add OrderItem with their Serial Numbers
    Public List<wrapperfetch> fetchseriallist {get; set;}
    // public Integer Counter;
    Public List<Order_Line_Item_Serials__c> insertordersn {get; set;} // list to add the selected orders
    Public map<id,Serial_Number__c> smap = new map<id,Serial_Number__c>();
    public List<Serial_Number__c> serialNumbers;
    public string fullname{get;set;}
    public string finalSearchValue{get;set;}
    
    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Enter the List');
    // ApexPages.addMessage(myMsg);
    
    Public fetchserials(){
        OrderItemList = new List<OrderItem>(); // intialize
        OrderVSerial = new Map<OrderItem,List<Serial_Number__c>>();
        fetchseriallist = new List<wrapperfetch>();
        insertordersn = new List<Order_Line_Item_Serials__c>();
        SelectedSerialList = new list<Serial_Number__c>();
        SerialList = new list<Serial_Number__c>();
        OrderID = ApexPages.currentPage().getParameters().get('Id'); //Getting the id of current order
      OrderInstance = [SELECT Id FROM Order where Id=:OrderID ];
       OrderItemList=[SELECT Id,OrderId,product2Id,PricebookEntry.Product2.Name, Quantity FROM OrderItem where OrderId =:OrderID];
       loadSerials();
    }
    //adding page reference of the current page
  /*  public PageReference step1() {
        loadSerials();
          if(SerialList==null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
'Please select the serial number'));

        return Page.fetchserials;
    }*/
    
    public PageReference cancel() {
        PageReference c=new PageReference('/'+OrderID);
        return c;
    }   
    
    Public void loadSerials(){
        ProductIdSet = new set<Id>();
        For(OrderItem items :OrderItemList){
            ProductIdSet.add(items.Product2Id);// order items products add into productidset 
        }
        SerialList = [SELECT Id,Serial_Number__c,Product_Name__c ,Name,Product__c,Status__c,Bin_Number__c,Location__c
                      FROM Serial_Number__c where Status__c=FALSE And Product__c In:ProductIdSet limit 200 ];
        system.debug('Serialx' +SerialList );
        
        /*if(SerialList == NULL)
{
SerialList.addError('There are no values in the Serials List.'); 
}*/
        // add error exception handling
        system.debug('Serialx' +SerialList );
        For(OrderItem item :OrderItemList){
            //counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id) // && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                 //     mahesh   fetchseriallist.add(new wrapperfetch(sn,item));
                 fetchseriallist.add(new wrapperfetch(sn));
                 SelectedSerialList.add(sn);
                 //counter = counter +1; 
                }
                else{continue;}
            }
            OrderVSerial.put(item,SelectedSerialList);
            /*=====
try{
if(SerialList == NULL ){ ApexPages.addMessages(e); }

catch (DMLException e){
ApexPages.addMessages(myMsg);
// Code for sending Email
}
============*/
        }
    }
    
    public void search(){
        finalSearchValue = '%' + fullname + '%';
       System.debug(finalSearchValue);
        fetchseriallist = new list<wrapperfetch>();
        List<Serial_Number__c> serialList =  [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Serial_Number__c like :finalSearchValue];
            System.debug(serialList.size());
        for(Serial_Number__c s : serialList){
                                      System.debug(s);
                                      fetchseriallist.add(new wrapperfetch(s));    
                                      System.debug(s);
                                  }
        
    }
    
    Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public String locatn{get;set;}
        Public String binNo{get;set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        //    Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
        Public wrapperfetch(Serial_Number__c sn){  
            //this.selected=check;
            //    this.Oitem= Oi;
            //    this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.locatn=sn.Location__c;
            this.binNo=sn.Bin_Number__c;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
            
        }
    }
    
    Public PageReference AddSerailNumbertoOrder(){
        
        for (wrapperfetch st: fetchseriallist ){
            
            If (st.selected == true ){
                Order_Line_Item_Serials__c ordersn= new Order_Line_Item_Serials__c();
                //Order_Line_Item_Serials__c make it to true
                //make a new Order_Line_Item_Serials__c list and make it to true.
                // put that into   ordersn.Serial_Number__c=st.stid;  a new list 
                ordersn.Serial_Number__c=st.stid;
                ordersn.Order_Product__c=st.opid;
                insertordersn.add(ordersn);
                
                Serial_Number__c  serials = new Serial_Number__c();
                serials.id = st.stid;
                serials.Status__c = TRUE;
                serials.Location__c=st.locatn;
                serials.Bin_Number__c = st.binNo;
                SelectedSerialList.add(serials);
                smap.put(serials.id,serials );
                
            }
            
        }
        if (insertordersn.size() > 0){    
                insert insertordersn;    
              //  update smap.values();    
            }
        return Page.fetchserials;    
    }       
}

Please help me to write test class for this code.

Thanks,
Mahesh
Best Answer chosen by Mahesh Babu 187
AbhishekAbhishek (Salesforce Developers) 
Mahesh Can you check the below blog's,

https://salesforce.stackexchange.com/questions/87533/writing-test-class-for-wrapper-class

https://www.forcetalks.com/salesforce-topic/how-to-cover-test-class-for-wrapper-class-in-salesforce/

Thanks.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Mahesh,

Write the test class based on this blog,

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

It might help you.

Thanks.
Mahesh Babu 187Mahesh Babu 187
Hi Abhishek,
Thanks for the quick reply.
I know how to write test class for Apex classes. But I have never written a test class for a wrapper class. But I have written the test class covering loadserials(). But I didn't understand how to write it for the search functionality here.
If possible help me to write test code for it.

Thanks,
Mahesh 
AbhishekAbhishek (Salesforce Developers) 
Mahesh Can you check the below blog's,

https://salesforce.stackexchange.com/questions/87533/writing-test-class-for-wrapper-class

https://www.forcetalks.com/salesforce-topic/how-to-cover-test-class-for-wrapper-class-in-salesforce/

Thanks.
This was selected as the best answer