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
RahulRahul 

Hi Friends, can you teach me how to cover this method?Thanks

User-added image
Sahil ShitoleSahil Shitole
Hi Rahul,

Can you please share your complete test class code?
RahulRahul
public with sharing class SeedsAllocationController {    

    public Integer bvcNumber{get;set;}
    public Primary_Crop__c pCrop{get;set;}
    public Boolean noCrop{get;private set;}
    public boolean showResult{get;private set;}
    public Id orderId{get;private set;}
    public Decimal totalFR{get;set;}
    public Decimal center{get;set;}
    public list<AllocationWrapper> wrapperList{get;set;}
     
    @testVisible
    class AllocationWrapper{
        
        public Allocation_Line_Item__c item{get;set;}
        public Decimal adjustment{get;set;}
        public boolean errorState{get;set;}
        public Decimal finalRemainingCent{get;set;}
        public Decimal centerInv{get;set;}
        
        public AllocationWrapper(Allocation_Line_Item__c item){
            this.item=item;
            this.adjustment=item.Adjustment__c.setScale(4);
            this.errorState=false;
            this.finalRemainingCent=0.00;
            this.centerInv=0.00;            
        }        
    }
    
    public SeedsAllocationController(){
        totalFR=0.00;
        center=0.00;
        noCrop=false;
        if(ApexPages.currentPage().getParameters().get('oid')!=null)
        {
            orderId=ApexPages.currentPage().getParameters().get('oid');   
        }
        
        if(ApexPages.currentPage().getParameters().get('bvc')!=null)
        {
            String x=ApexPages.currentPage().getParameters().get('bvc');
            if(x.isNumeric())
            {
                bvcNumber=Integer.valueOf(x);
                searchPrimaryCrop();
            }
        }
    }
    
    public void searchPrimaryCrop()
    {
        //this method searches for bvc number to find primary crop
        noCrop=false;
        showResult=false;
        wrapperList=new list<AllocationWrapper>();
        try
        {
            pCrop=[SELECT id,bvc__c,name,Reserved_Stock__c,Current_Inventory__c,Center__c,Total_Allocated_Quantity__c,Total_Adjustment__c,SumTotalActual__c,SumTotalForecast__c,
            TotalRemaining__c,TotalFinalRemaining__c,Total_Blocked_Inventory__c,Total_Allocation_Items__c,(SELECT id,Total_Forecast__c,Total_Actual__c,Remaining__c,
            Qty_Allocated__c,Final_Remaining__c,Salesman__r.Name,salesman__c,Adjustment__c,Final_Remaining_Percent__c,BlockedAllocation__c FROM Allocation_Line_Items__r 
            WHERE Salesman__r.isActive=true order by Salesman__r.Name) FROM primary_crop__c where bvc__c=:bvcNumber limit 1];
            
            showResult=true;
            totalFR=pCrop.TotalFinalRemaining__c.setScale(4);
            center=pCrop.Center__c.setScale(4);
            for(Allocation_Line_Item__c item:pCrop.Allocation_Line_Items__r)
            {
                wrapperList.add(new AllocationWrapper(item)); 
            }
        }
        catch(System.QueryException e)
        {
            noCrop=true;
            showResult=false;
        }
    }
    
    public PageReference updateStock()
    {
        //this method updates stock and allocation
        Savepoint sp=Database.setSavepoint();
         Boolean errorState = false;

        try
        {    
        
            Decimal totalAdjust=0.00;
            
            for(AllocationWrapper wrap : wrapperList)
            {
                //check if allocated quantity is not less than zero
                
                System.debug('wrap.item.Qty_Allocated__c :'+ wrap.item.Qty_Allocated__c + 
                'wrap.adjustment.setScale(4) :'+ wrap.adjustment.setScale(4)+ 
                'wrap.item.Adjustment__c.setScale(4) : ' + wrap.item.Adjustment__c.setScale(4));
                
                totalAdjust+=wrap.adjustment.setScale(4);
                
                if(wrap.item.Qty_Allocated__c+wrap.adjustment.setScale(4)<0){
                    System.debug('#@#@ Allocated Quantity(if(wrap.item.Qty_Allocated__c+newAdj<0)): '+wrap.item.Qty_Allocated__c+wrap.adjustment.setScale(4));
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Allocated quantity cannot be less than zero'));
                    errorState=true;
                }
             }
             
             if(totalAdjust!=0){
                 System.debug('#@#@ total adjustment(inside if): '+totalAdjust);
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Total Adjustment should be zero'));
             }
             if(totalAdjust==0 && errorState == False){
                list<Allocation_Line_Item__c> aitemList=new list<Allocation_Line_Item__c>();
               //if there is no error update allocation line items and allocate new qty
               for(AllocationWrapper wrap : wrapperList){
                  wrap.item.Qty_Allocated__c+=wrap.adjustment.setScale(4);
                 aitemList.add(wrap.item);
                 //System.debug('#item '+wrap.item);
               }
               
               update aitemList;
               searchPrimaryCrop();                
             }  
             errorState = False;      
             System.debug(wrapperList);
                    
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,e.getMessage()));
            Database.rollback(sp); 
            
        }
        return null;        
    }
    
    public PageReference allocateInventory()
    {
        if(pCrop.Reserved_Stock__c>pCrop.Current_Inventory__c)
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Reserved Stock cannot be greater than current inventory'));
                return null;
            }
            
        Else{
             update pCrop;
            //this method re-allocates inventory if total quantity to allocate is less than current total allocated quantity
            list<Allocation_Line_Item__c> itemList=new list<Allocation_Line_Item__c>();
            Decimal qty=pCrop.Current_Inventory__c.setScale(4)-pCrop.Reserved_Stock__c.setScale(4)-pCrop.Total_Blocked_Inventory__c.setScale(4);
            Decimal totalAdjust=0;
            
            if(qty>0 && pCrop.sumTotalForecast__c>0)
            {
                for(AllocationWrapper wrap:wrapperList)
                {
                    //wrap.item.Adjustment__c=wrap.adjustment.setScale(4);
                    //wrap.item.Final_Remaining__c=wrap.item.Remaining__c+wrap.item.Adjustment__c.setScale(4);
                    wrap.finalRemainingCent=wrap.item.Total_forecast__c.setScale(4)/pCrop.SumTotalForecast__c.setScale(4);
                    //wrap.item.Qty_Allocated__c=Math.min(wrap.item.Final_Remaining__c, wrap.finalRemainingCent*qty).setScale(4);
                    wrap.item.Qty_Allocated__c = wrap.finalRemainingCent*qty.setScale(4);
                    itemList.add(wrap.item);
                }
                
            }
            
            else if (qty>0 && pCrop.sumTotalForecast__c==0){
                for(AllocationWrapper wrap:wrapperList)
                {
                    wrap.item.Qty_Allocated__c= qty/pCrop.Total_Allocation_Items__c.setScale(4);
                    itemList.add(wrap.item);
                }
            }
            
            else
            {
                for(AllocationWrapper wrap:wrapperList)
                {
                    wrap.item.Qty_Allocated__c= 0.00;
                    itemList.add(wrap.item);
                }
                
            }
            update itemList;
            searchPrimaryCrop();
            return null;
        }
    }
    
    public void allocate()
    {
        //this method allocates inventory from center to salesmanager
        Decimal total=0.00;
        for(AllocationWrapper wrap:wrapperList){
            total+=wrap.centerInv.setScale(4);
        }
        
        if(total<=center)
        {
            list<Allocation_Line_Item__c> aitemList=new list<Allocation_Line_Item__c>();
            for(AllocationWrapper wrap:wrapperList)
            {
                wrap.item.Qty_Allocated__c+=wrap.centerInv.setScale(4);
                wrap.centerInv=0.00;
                aitemList.add(wrap.item);
            }
            update aitemList;
            searchPrimaryCrop();
        }
        else
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Total Allocation from center cannot be greater than center'));
        }
    } 

    public PageReference goToOrder() 
    {
        return new PageReference('/apex/AllocationCheck?id='+orderId);
    }
}
 
RahulRahul
This is the test class:- got 67% coverage 


@isTest
public class SeedsAllocationControllerTest{
    
    @testSetup private static void setupData(){
        list<Primary_Crop__c> pcList=new list<Primary_Crop__c>();
        Primary_Crop__c pc1=new Primary_Crop__c();
        pc1.BVC__c=1;
        pc1.Name='Crop1';
        pc1.Current_Inventory__c=400.00;
        pc1.Reserved_Stock__c=10;
        pcList.add(pc1);
        
        Primary_Crop__c pc2=new Primary_Crop__c();
        pc2.BVC__c=2;
        pc2.Name='Crop2';
        pc2.Current_Inventory__c=400.00;
        pc2.Reserved_Stock__c=300;
        pcList.add(pc2);
        
        
        Primary_Crop__c pc3=new Primary_Crop__c();
        pc3.BVC__c=3;
        pc3.Name='Crop3';
        pc3.Current_Inventory__c=400.00;
        pc3.Reserved_Stock__c=0;
        pcList.add(pc3);
        
        insert pcList;
        
        delete [select id from Allocation_Line_Item__c];
        
        Test.startTest();
        TestDataFactory.createUser('Sales Manager',5);
        Test.stopTest();
        
        
        
        list<Allocation_Line_Item__c> aitemList1=[select id,Final_Remaining__c,Final_Remaining_Percent__c,BlockedAllocation__c,Salesman__c,Primary_Crop__r.TotalFinalRemaining__c from Allocation_Line_Item__c where Primary_Crop__c=:pc1.id and salesman__r.email like '%testuser.com'];
        for(Allocation_Line_Item__c item:aitemList1){
            item.Total_Forecast__c=1000;
            item.Total_Actual__c=300;
        }
        update aitemList1;
        
        list<Allocation_Line_Item__c> aitemList2=[select id from Allocation_Line_Item__c where Primary_Crop__c=:pc2.id and salesman__r.email like '%testuser.com'];
        for(Allocation_Line_Item__c item:aitemList2){
            item.Total_Forecast__c=1000;
            item.Total_Actual__c=300;
        }
        update aitemList2;
        InventoryAllocatorBatch myBatch=new InventoryAllocatorBatch();
        Database.executeBatch(myBatch);
    }
    
    private static testmethod void test1(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPage(pr);
        pr.getParameters().put('bvc','1');
        SeedsAllocationController ctr=new SeedsAllocationController();
        list<SeedsAllocationController.AllocationWrapper> testWrap=ctr.wrapperList;
        System.assertEquals(5,testWrap.size(),'Size of list is not as expecetd');
        testWrap[0].adjustment=100;
        ctr.updateStock();
        testWrap[1].adjustment=-100;
        ctr.updateStock();
        for(SeedsAllocationController.AllocationWrapper x:testWrap){
            System.debug(x);
        }
        
    }
    
    private static testmethod void noCrop(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPage(pr);
        pr.getParameters().put('bvc','4');
        SeedsAllocationController ctr=new SeedsAllocationController();
    }
    
    private static testmethod void invalidBVC(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPage(pr);
        pr.getParameters().put('bvc','a');
        SeedsAllocationController ctr=new SeedsAllocationController();
        
    }
    
  
    private static testmethod void backToOrder(){
        Order__c o=TestDataFactory.getOrder();
        insert o;
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('oid',o.id);
        SeedsAllocationController ctr=new SeedsAllocationController();
        ctr.goToOrder();
    }
    
    private static testmethod void positiveTest(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        SeedsAllocationController ctr=new SeedsAllocationController();
        ctr.bvcNumber=1;
        ctr.searchPrimaryCrop();
        list<SeedsAllocationController.AllocationWrapper> testWrap=ctr.wrapperList;
        testWrap[0].adjustment=10;
        testWrap[1].adjustment=-10;
        ctr.updateStock();
    }
    
  /*  private static testmethod void allocationModel2(){
        System.debug('##########allocationmodel2test');
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('bvc','1');
        SeedsAllocationController ctr=new SeedsAllocationController();
        ctr.pCrop.Reserved_Stock__c=40;
        ctr.updateStock();
        System.assertEquals(40, ctr.pCrop.Reserved_Stock__c,'Reserved Stock of Crop is not updated');
        
    } */
    
    private static testmethod void allReserved(){
        System.debug('##########allocationmodel2test');
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('bvc','1');
        SeedsAllocationController ctr=new SeedsAllocationController();
        ctr.pCrop.Reserved_Stock__c=400;
        ctr.updateStock();
        
    }
    
    private static testmethod void allocationmodel2invalidadjustment(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('bvc','1');
        SeedsAllocationController ctr=new SeedsAllocationController();
        list<SeedsAllocationController.AllocationWrapper> testWrap=ctr.wrapperList;
        testWrap[0].adjustment=30;
        ctr.pCrop.Reserved_Stock__c=40;
        ctr.updateStock();
    }
    
    private static testmethod void allocatefromcentererror(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('bvc','1');
        SeedsAllocationController ctr=new SeedsAllocationController();
        list<SeedsAllocationController.AllocationWrapper> testWrap=ctr.wrapperList;
        System.debug('center :'+ctr.pCrop.center__c);
        testWrap[0].centerInv=10;
        ctr.allocate();
    
    }
    
     private static testmethod void allocatefromcenter(){
        PageReference pr=Page.SeedsAllocationTool;
        Test.setCurrentPageReference(pr);
        pr.getParameters().put('bvc','3');
        SeedsAllocationController ctr=new SeedsAllocationController();
        list<SeedsAllocationController.AllocationWrapper> testWrap=ctr.wrapperList;
        System.debug('center :'+ctr.pCrop.center__c);
        testWrap[0].centerInv=100;
        ctr.allocate();
    
    }
    
    private static testmethod void allocateinventorytest(){
    
     list<Primary_Crop__c> pcList=new list<Primary_Crop__c>();

        Primary_Crop__c pc2=new Primary_Crop__c();
        pc2.BVC__c=2;
        pc2.Name='Crop2';
        pc2.Current_Inventory__c=400.00;
        pc2.Reserved_Stock__c=300;
        pcList.add(pc2); 
        
        if(pc2.Reserved_Stock__c >  pc2.Current_Inventory__c)   {
        
        SeedsAllocationController SAC = new SeedsAllocationController();
        SAC.allocateInventory();
            
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'Reserved Stock cannot be greater than current inventory'));
                
        }
    
    }
    
}
Sahil ShitoleSahil Shitole
Hi Rahul,

Please use below test methods to cover SeedsAllocationController.allocateInventory() method:

private static testmethod void allocateinventorytest1(){
    Primary_Crop__c pc2=new Primary_Crop__c();
    pc2.BVC__c=2;
    pc2.Name='Crop2';
    pc2.Current_Inventory__c=4000.00;
    pc2.Reserved_Stock__c=300;
    pc2.Total_Blocked_Inventory__c = 100;
    pc2.sumTotalForecast__c=500;
    PageReference pr=Page.SeedsAllocationTool;
    Test.setCurrentPage(pr);
    SeedsAllocationController SAC = new SeedsAllocationController();
    System.Test.startTest();
    SAC.pCrop = pc2;
    SAC.allocateInventory();
    System.Test.stopTest();
  }
}
private static testmethod void allocateinventorytest2(){
    Primary_Crop__c pc2=new Primary_Crop__c();
    pc2.BVC__c=2;
    pc2.Name='Crop2';
    pc2.Current_Inventory__c=4000.00;
    pc2.Reserved_Stock__c=300;
    pc2.Total_Blocked_Inventory__c = 100;
    pc2.sumTotalForecast__c=0;
    PageReference pr=Page.SeedsAllocationTool;
    Test.setCurrentPage(pr);
    SeedsAllocationController SAC = new SeedsAllocationController();
    System.Test.startTest();
    SAC.pCrop = pc2;
    SAC.allocateInventory();
    System.Test.stopTest();
  }
}
private static testmethod void allocateinventorytest3(){
    Primary_Crop__c pc2=new Primary_Crop__c();
    pc2.BVC__c=2;
    pc2.Name='Crop2';
    pc2.Current_Inventory__c=0;
    pc2.Reserved_Stock__c=0;
    pc2.Total_Blocked_Inventory__c = 0;
    pc2.sumTotalForecast__c=0;
    PageReference pr = Page.SeedsAllocationTool;
    Test.setCurrentPage(pr);
    SeedsAllocationController SAC = new SeedsAllocationController(); 
    System.Test.startTest();
    SAC.pCrop = pc2;
    SAC.allocateInventory();
    System.Test.stopTest();
  }
}
private static testmethod void allocateinventorytest4(){
    Primary_Crop__c pc2=new Primary_Crop__c();
    pc2.BVC__c=2;
    pc2.Name='Crop2';
    pc2.Current_Inventory__c=100;
    pc2.Reserved_Stock__c=500;
    PageReference pr = Page.SeedsAllocationTool;
    Test.setCurrentPage(pr);
    SeedsAllocationController SAC = new SeedsAllocationController();
    System.Test.startTest();
    SAC.pCrop = pc2;
    SAC.allocateInventory();
    System.Test.stopTest();
  }
}

Do let me know if this helps you.

Thanks


 
Sahil ShitoleSahil Shitole
Hi Rahul,

Was my solution helpful?

Thanks