+ Start a Discussion
pdostpdost 

Test Class Support (Only at 56%)

I am trying to get complete coverage for my test class and cannot get there as I cannot get passed 56%.  Can anyone point me in the right direction?

 

Class

public class oitemController
{
    public List<Product_Bundle__c> lstbundle = new List<Product_Bundle__c>();
    public List<Order_Item_Junction__c> lstoitem {get;set;}
    public ID<Order__c> oid {get;set;}
    public string orderid;
    public integer iqty;
    public boolean proceed {get;set;}
    
    public boolean setproceed()
    {
        return true;
    }
    
    public PageReference saveitems()
    {
        //Save item relationships
        List<Order_Item_Junction__c> insitems = new List<Order_Item_Junction__c>();
        try{
            for(integer i=0;i<lstoitem.size();i++)
                {
                if(lstoitem[i].Quantity__c > 0)
                    {
                    Order_Item_Junction__c qtyitem = new Order_Item_Junction__c();
                    qtyitem.Order__c = oid;
                    qtyitem.Product_Bundle__c = lstbundle[i].Id;
                    qtyitem.Quantity__c = lstoitem[i].Quantity__c;
                    insitems.add(qtyitem);
                    }
                }
            if(insitems.size()>0)
                insert insitems;
        }catch(Exception ex){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Error:'+ ex.getMessage()));
            System.debug(ex.getMessage());
        }
        return null; 
    }
    
    public oitemController(ApexPages.StandardController controller)
    {
        //Order ID
        orderid = ApexPages.currentPage().getparameters().get('oid');
        //Record Type
        string orecordtype = ApexPages.currentPage().getparameters().get('rectype');
        
            if(orecordtype == 'New Order')
            {            
                //Fetch all Product Bundles where Order_Availability__c = New Order
                lstbundle = [Select Order_Availability__c, Name, Id From Product_Bundle__c where Order_Availability__c =:'New Order' and Completed__c =: TRUE order by Name];
            }
            else if(orecordtype == 'Return Order')
            {
                //Fetch all Product Bundles where Order_Availability__c = Return Oder
                lstbundle = [Select Order_Availability__c, Name, Id From Product_Bundle__c where Order_Availability__c =:'Return Order' and Completed__c =: TRUE order by Name];
            }
        
        lstoitem = new List<Order_Item_Junction__c>();
        oid = orderid;
        
        try{
        for(integer i=0;i<lstbundle.size();i++)
        {
            Order_Item_Junction__c oitem = new Order_Item_Junction__c();
            oitem.Product_Bundle__c = lstbundle[i].Id;
            oitem.Order__c = oid;
            oitem.Quantity__c = 0;
            lstoitem.add(oitem);
            
        }
        }catch(Exception ex){
            // Adding exeption message onto the page
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Error:'+ ex.getMessage()));
            System.debug(ex.getMessage());
        }
    }   
}

 

 

Page

<apex:page id="orderpage1" standardController="Order_Item_Junction__c" extensions="oitemController" sidebar="false" showheader="false" >
<script language="JavaScript" type="text/javascript">
function CloseAndRefresh()
{
    window.opener.location.href="/{!$CurrentPage.parameters.oid}";
    window.top.close();
      
}
</script>
    <apex:form id="form1">
        <apex:pageBlock title="Add Order Items" id="order">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Save" action="{!saveitems}" oncomplete="CloseAndRefresh()" />
                <apex:commandButton value="Cancel" onclick="CloseAndRefresh()" />
            </apex:pageBlockButtons>
                <apex:pageBlockSection id="pbsbundle" title="Available Product Bundles" rendered="true" columns="1">
                    <apex:pageBlockTable id="pbtbundles" value="{!lstoitem}" var="l" width="100%">
                        <apex:column headerValue="Order" value="{!l.Order__c}" width="40%"/>
                        <apex:column headervalue="Product Bundle Name" value="{!l.Product_Bundle__c}" width="40%"/>
                        <apex:column headerValue="Quantity" id="iqty" width="20%">
                            <apex:inputField value="{!l.Quantity__c}"/>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!saveitems}" oncomplete="CloseAndRefresh()" />
                <apex:commandButton value="Cancel" onclick="CloseAndRefresh()" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Test Class

@isTest
private class oitemControllerTest
{
    static testmethod void testorderNew1()
    {
        delete [Select Order_Availability__c, Name, Id From Product_Bundle__c where Order_Availability__c =:'New Order' and Completed__c =: TRUE order by Name];

        RecordType recnew = [select ID, Name from RecordType where Name = 'New Order' and SObjectType = 'Order__c'];
        RecordType recreturn = [select ID, Name from RecordType where Name = 'Return Order' and SObjectType = 'Order__c'];
        
        Order__c order1 = new Order__c(recordtypeid=recnew.Id);
        insert order1;
        
        Order__c order2 = new Order__c(recordtypeid=recreturn.Id);
        insert order2;
        
        RecordType recopen = [select ID, Name from RecordType where Name = 'Open' and SObjectType = 'Product_Bundle__c'];
        RecordType recreturnbun = [select ID, Name from RecordType where Name = 'Open' and SObjectType = 'Product_Bundle__c'];
        
        Product_Bundle__c bundle1 = new Product_Bundle__c(recordtypeid = recopen.id, Name = 'TestBundle1', Description__c = 'Testing1', Completed__c = TRUE, Order_Availability__c = 'New Order');
        Product_Bundle__c bundle2 = new Product_Bundle__c(recordtypeid = recreturnbun.id, Name = 'TestBundle2', Description__c = 'Testing2', Completed__c = TRUE, Order_Availability__c = 'Return Order');
        Product_Bundle__c bundle3 = new Product_Bundle__c(recordtypeid = recopen.id, Name = 'TestBundle3', Description__c = 'Testing3', Completed__c = FALSE, Order_Availability__c = 'New Order');
        Product_Bundle__c bundle4 = new Product_Bundle__c(recordtypeid = recreturnbun.id, Name = 'TestBundle1', Description__c = 'Testing4', Completed__c = FALSE, Order_Availability__c = 'Return Order');
        Product_Bundle__c [] bundle_array = new Product_Bundle__c []{bundle1,bundle2,bundle3,bundle4};
        insert bundle_array;
        
        Order_Item_Junction__c oi1 = new Order_Item_Junction__c(Order__c = order1.Id,Product_Bundle__c = bundle1.id, Quantity__c = 1);
        Order_Item_Junction__c oi2 = new Order_Item_Junction__c(Order__c = order1.Id,Product_Bundle__c = bundle2.id, Quantity__c = 2);
        Order_Item_Junction__c oi3 = new Order_Item_Junction__c(Order__c = order2.Id,Product_Bundle__c = bundle3.id, Quantity__c = 1);
        Order_Item_Junction__c oi4 = new Order_Item_Junction__c(Order__c = order2.Id,Product_Bundle__c = bundle4.id, Quantity__c = 2);        
        Order_Item_Junction__c [] oi_array = new Order_Item_Junction__c []{oi1,oi2};
        insert oi_array;
        
        PageReference pref1 = Page.orderpage;
        pref1.getParameters().put('oid',order1.Id);
        pref1.getParameters().put('orecordtype',recnew.Name);
        Test.setCurrentPage(pref1);
        
        ApexPages.StandardController stdcon1 = new ApexPages.StandardController(oi_array[0]);
        oitemController oicon1 = new oitemController(stdcon1);
        oicon1.setproceed();
        oicon1.saveitems();

        PageReference pref2 = Page.orderpage;
        pref2.getParameters().put('oid',order2.Id);
        pref2.getParameters().put('orecordtype',recreturn.Name);
        pref2.getParameters().put('orecordtype',recreturn.Name);
        Test.setCurrentPage(pref2);
        
        ApexPages.StandardController stdcon2 = new ApexPages.StandardController(oi_array[5]);
        oitemController oicon2 = new oitemController(stdcon2);
        oicon1.setproceed();
        oicon1.saveitems();
            
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JaggyJaggy

pdost,

 

Are you running your test cases from UI or from eclipse?

I suggest you to run your test cases from UI. After running your test cases from UI, scroll down to the class of which you wanted to see code coverage. Now click on the class link then a window pops up. This window shows which areas are not covered by the test method in red color. Now you can figure out what logic you are missing in your test method. I hope this will help.