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
Alex MakkAlex Makk 

Test Class help - Wrapper decimal is not getting set with a new value

Hello, I have a simple controller that I'd like to test. And the issue is that 'wrapobj.toAdjust' is not getting new value within test class. I'm relatively new to this - please advise. Thank you!

Controller:
public  with Sharing class INV_Audit_tool {
    
    Id whid;
    public list<wrapinv> wrapinvList { get; set; }
    public String WhName { get; set; }
 
    public INV_Audit_tool(){
        whid=ApexPages.CurrentPage().getparameters().get('id'); 
        //system.debug('whid==>'+whid);
        wrapinvList = new list<wrapinv>();
        
        List<Inventory__c> invList= [SELECT Id, WH_Real_Available__c, Product__c,  Product__r.Name, Product__r.Family, Warehouse_Location__c, Warehouse_Location__r.Name
                              FROM Inventory__c 
                              WHERE Warehouse_Location__c=:whid 
                              AND (Product__r.Family != 'COG PPE' AND Product__r.Family != 'COG PPE Scrap') 
                              ORDER BY Product__r.Name ASC];
        
        //system.debug('invList.size()==>'+invList.size());
        for(Inventory__c i : invList){
            
            wrapinvList.add(new wrapinv(i, 0));
            WhName = i.Warehouse_Location__r.Name;
        }
    }
    
    public PageReference save(){
        List <Inventory_Transaction__c > tranToInsert = new List <Inventory_Transaction__c >();
        for(wrapinv wrapobj : wrapinvList){
        system.debug('@@@@@ '+wrapobj);
            if(wrapobj.toAdjust > 0){
                 decimal difference = wrapobj.inv.WH_Real_Available__c - wrapobj.toAdjust;
                 
                 Inventory_Transaction__c tempInvTransObj = new Inventory_Transaction__c();
                 tempInvTransObj.Product__c = wrapobj.inv.Product__c;
                 tempInvTransObj.Warehouse_Location1__c = whid;
                 tempInvTransObj.Quantity__c = wrapobj.toAdjust;
                 tempInvTransObj.Overwrite_Existing_QoH__c  = true;
                 tempInvTransObj.Message__c = 'Audit: Old Quantity ' + wrapobj.inv.WH_Real_Available__c + ' Difference after audit: ' +  difference;
                 tranToInsert.add(tempInvTransObj);  
            }
        }
        try{ 
            if(tranToInsert.size()>0){
                insert tranToInsert;
            }
        }catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,''+e.getMessage()));   
        }
        PageReference redirectPG;
        redirectPG = new PageReference('/'+whid);
        return redirectPG;
    }
    
     public PageReference Cancel(){
        PageReference redirectPG;
        redirectPG = new PageReference('/'+whid);
        return redirectPG;
    } 
    
    public class wrapinv{
        public Inventory__c inv{get;set;}
        public decimal toAdjust{get;set;}
        
        public wrapinv(Inventory__c  i, Decimal adj){
            inv=i;
            toAdjust = adj;
        }
    }
}

Test:
@isTest
private class INV_Audit_tool_test {
    static testMethod void INV_Audit_tool() {
    
    Warehouse_Location__c wh = new Warehouse_Location__c();
    wh.name = 'test';
    insert wh;
    
    product2 prod = new product2();
    prod.Name = 'Test';
    prod.family = 'Absorbents 16Oz';
    insert prod;
    
    Inventory__c inv = new Inventory__c();
    inv.product__c = prod.id;
    inv.Qty_on_Hand__c = 100;
    inv.Qty_allocated__c = 0;
    inv.Warehouse_Location__c = wh.id;
    insert inv;
    
    
       
    test.StartTest();
    
    PageReference pageRef = new PageReference('/apex/INV_Audit_tool');
        pageRef.getParameters().put('id' , wh.id);
        Test.setCurrentPageReference(pageRef);
        INV_Audit_tool audit = new  INV_Audit_tool();

        INV_Audit_tool.wrapinv whObj = new INV_Audit_tool.wrapinv(inv, 5);
      
        audit.save();
        audit.cancel();
       
    test.StopTest();
    
    }
}
Here is test result: wrapobj.ToAdjust is not getting test value of 5. It stays 0.

User-added image
​​​​​​​
Raj VakatiRaj Vakati
Your controller looks like incorrect .. 

1 :You are setting the zero allways in your constructor so its woring


            wrapinvList.add(new wrapinv(i, 0));

In your save you are check the below condition that will not execute at all 



            if(wrapobj.toAdjust > 0){


Please change those two condition 

use this controller and test class
 
public  with Sharing class INV_Audit_tool {
    
    Id whid;
    public list<wrapinv> wrapinvList { get; set; }
    public String WhName { get; set; }
 
    public INV_Audit_tool(){
        whid=ApexPages.CurrentPage().getparameters().get('id'); 
        //system.debug('whid==>'+whid);
        wrapinvList = new list<wrapinv>();
        
        List<Inventory__c> invList= [SELECT Id, WH_Real_Available__c, Product__c,  Product__r.Name, Product__r.Family, Warehouse_Location__c, Warehouse_Location__r.Name
                              FROM Inventory__c 
                              WHERE Warehouse_Location__c=:whid 
                              AND (Product__r.Family != 'COG PPE' AND Product__r.Family != 'COG PPE Scrap') 
                              ORDER BY Product__r.Name ASC];
        
        //system.debug('invList.size()==>'+invList.size());
        for(Inventory__c i : invList){
            
            wrapinvList.add(new wrapinv(i, i.WH_Real_Available__c));
            WhName = i.Warehouse_Location__r.Name;
        }
    }
    
    public PageReference save(){
        List <Inventory_Transaction__c > tranToInsert = new List <Inventory_Transaction__c >();
        for(wrapinv wrapobj : wrapinvList){
        system.debug('@@@@@ '+wrapobj);
            if(wrapobj.toAdjust > 0){
                 decimal difference = wrapobj.inv.WH_Real_Available__c - wrapobj.toAdjust;
                 
                 Inventory_Transaction__c tempInvTransObj = new Inventory_Transaction__c();
                 tempInvTransObj.Product__c = wrapobj.inv.Product__c;
                 tempInvTransObj.Warehouse_Location1__c = whid;
                 tempInvTransObj.Quantity__c = wrapobj.toAdjust;
                 tempInvTransObj.Overwrite_Existing_QoH__c  = true;
                 tempInvTransObj.Message__c = 'Audit: Old Quantity ' + wrapobj.inv.WH_Real_Available__c + ' Difference after audit: ' +  difference;
                 tranToInsert.add(tempInvTransObj);  
            }
        }
        try{ 
            if(tranToInsert.size()>0){
                insert tranToInsert;
            }
        }catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,''+e.getMessage()));   
        }
        PageReference redirectPG;
        redirectPG = new PageReference('/'+whid);
        return redirectPG;
    }
    
     public PageReference Cancel(){
        PageReference redirectPG;
        redirectPG = new PageReference('/'+whid);
        return redirectPG;
    } 
    
    public class wrapinv{
        public Inventory__c inv{get;set;}
        public decimal toAdjust{get;set;}
        
        public wrapinv(Inventory__c  i, Decimal adj){
            inv=i;
            toAdjust = adj;
        }
    }
}
 
@isTest
private class INV_Audit_tool_test {
    static testMethod void INV_Audit_tool() {
    
    Warehouse_Location__c wh = new Warehouse_Location__c();
    wh.name = 'test';
    insert wh;
    
    product2 prod = new product2();
    prod.Name = 'Test';
    prod.family = 'Absorbents 16Oz';
    insert prod;
    List<Inventory__c> invList = new List<Inventory__c>();
	for(integer i =0 ; i<10 ; i++){
    Inventory__c inv = new Inventory__c();
    inv.product__c = prod.id;
    inv.Qty_on_Hand__c = 100;
    inv.Qty_allocated__c = 0;
    inv.Warehouse_Location__c = wh.id;
	invList.add(inv);
	}
    //insert inv;
    
	insert invList ; 
    
       
    Test.StartTest();
    

 
PageReference pageRef = Page.INV_Audit_tool; // Add your VF page Name here
    pageRef.getParameters().put('id' , wh.id);
        Test.setCurrentPageReference(pageRef);
		
        INV_Audit_tool audit = new  INV_Audit_tool();
        audit.save();
        audit.cancel();
       
    Test.StopTest();
    
    }
}


 
Alex MakkAlex Makk
Hi Raj, thanks for your reply. Yes, I'm setting toAdjust to 0 to prevent unnecessary adjustments. If a user changes toAdjust to a value higher than 0 - script should work and create records.