0

I have give two methods selctedInvs and InvQtyChange and skip other for simplicity. selctedInvs covered successfully the problem is to cover InvQtyChange. In that wrapInvList receives 0 for Qty as that is set to 0 in selctedInvs but in visualforce page it changed dynamically that I can't do in test class.So for loop in InvQtyChange is not covered. How to overcome this.

controller:

public class TrasferPartsController{

public PageReference selctedInvs() {
    if(transTypeWH || transTypeVH){

        String selectedInv=String.valueOf(apexpages.currentpage().getparameters().get('selInv'));
        Inventory__c invl;
        if(selectedInv != null){
            Invl=[select id,Name,Product__r.name,Total_Received__c,Warehouse__c,Warehouse_Location__c from Inventory__c where id=:selectedInv]; 
        }
        list<wrapTran> wraptranList =new list<wrapTran>();
        wrapInvList.add(new WrapInv(String.valueOf(invl.Product__r.name),wraptranList,0,Integer.valueOf(invl.Total_Received__c))); 
        system.debug('wrapInvList======'+wrapInvList);
    }
    else{
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Select Warehouse/Vehicle first');
        ApexPages.addMessage(myMsg);
    }
    return null;
}



public PageReference InvQtyChange() {

    String selectedInv=String.valueOf(apexpages.currentpage().getparameters().get('qtyPrd'));

    wrapTran temp;
    for(WrapInv serW : wrapInvList){  
        if(serW.PrdName == selectedInv){
            if(serW.Qty <= serW.AvailQty){
                serW.tranList.clear();

                for(integer m=0;m<serW.Qty;m++){

                     temp=new wrapTran(serW.PrdName,'',null);
                    serW.tranList.add(temp);
                }

            } 
            else{
                serW.Qty=0;
            }                                    
         }
    }
    return null;
} 


public class WrapInv{
   public list<wrapTran> tranList{get;set;}
   public Integer Qty{get;set;}
   public String PrdName{get;set;}
   public Integer AvailQty{get;set;}

   public WrapInv(String pname,List<wrapTran> LI,Integer sn,Integer avail){
       AvailQty=avail;
       PrdName=pname;
       tranList=LI;
       system.debug('translist========'+tranList);
       Qty=sn;
   }


Visualforce page:

 <apex:pageBlockTable value="{!wrapInvList}" var="invs">
              <apex:column headerValue="Product" value="{!invs.PrdName}"/>
              <apex:column headerValue="Recived Quantity" value="{!invs.AvailQty}"/>
              <apex:column headerValue="Quantity to Transfer" >
                        <apex:actionRegion ><apex:inputtext value="{!invs.Qty}">
                            <apex:actionSupport event="onchange" reRender="invPrd" action="{!InvQtyChange}">
                                <apex:param name="qtyPrd" value="{!invs.PrdName}"/>
                            </apex:actionSupport>
                        </apex:inputtext></apex:actionRegion>
                    </apex:column>


Test class:

@isTest
    public class TrasferPartsControllerTest{

    static testMethod void myTest(){

    test.starttest();

    //perform required operation before this

    TrasferPartsController TP=new TrasferPartsController();
    TP.selctedInvs();
    TP.InvQtyChange();

    test.stoptest();
    }
}
  • You set the qty via code in your test method. Replicating what the user does on the page. Wrapinvlist[x].qty=xyz – Eric Dec 12 '16 at 16:22
1

You will need to set the required properties and page parameters expected by the controller in the test case. E.g.

@isTest
public class TrasferPartsControllerTest{

    static testMethod void myTest(){

        list<TrasferPartsController.WrapInv> wrapInvList = new list<TrasferPartsController.WrapInv>();
        // TODO: Complete WrapInv() constructor call with required params
        TrasferPartsController.WrapInv wrapI = new TrasferPartsController.WrapInv();
        wrapInvList.add(wrapI);

        TrasferPartsController TP = new TrasferPartsController();
        TP.wrapInvList = wrapInvList;

        system.currentPageReference().getParameters().put('qtyPrd', wrapInvList[0].PrdName);

        test.starttest();
        TP.InvQtyChange();
        test.stoptest();

        // TODO: Make some assertions about the expected changes to tranList and Qty
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.