+ Start a Discussion
ArunKumar_RemmaArunKumar_Remma 

How to write a Test Case

Hi All,

 

    We have written an apex class for multiple line items, but facing problem in writing the test case for the same.  So, could any one please suggest a solution to the making the test case successful. 

 

 

Apex Code:

 

 

public class DEV_MULTIPLE_InvoiceLineEntry {

    public List<Invoice_Line_Item__c> ords {get; set;}
    private final Invoice__c parOrd;
    public DEV_MULTIPLE_InvoiceLineEntry(ApexPages.StandardController myController) {
        parOrd=(Invoice__c)myController.getrecord();
        ords = new List<Invoice_Line_Item__c>();
         // Get the Current User
     user u1 =[select id ,name,Formula_Warehouse__c from user where id=:UserInfo.getUserId()];
         //Get The DefaultwareHouse for the user
     Warehouse__c[] w1 =[select id,name from Warehouse__c where name =: u1.Formula_Warehouse__c or name = null];
     
     // Condition to Check if Formula_Warehouse__c =null, if null Item_Warehouse__c on page is NULL
        if(u1.Formula_Warehouse__c ==null){
        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
      
        LitOrd.Invoice_Number__c = parOrd.id;
        LitOrd.quantity__c=1;
       
        ords.add(LitOrd);
         
        }
        // Condition to Check if Formula_Warehouse__c !=null, if ! null Item_Warehouse__c on page is warehouse id(only ids display names in Lookups)
        else if(u1.Formula_Warehouse__c !=null){
        
        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
      
        LitOrd.Invoice_Number__c = parOrd.id;
        LitOrd.quantity__c=1;
        LitOrd.Item_Warehouse__c =w1[0].id;
        ords.add(LitOrd);
        }
        }
        
       

    public void addrow() {
    
        // Get the Current User
     user u =[select id ,name,Formula_Warehouse__c from user where id=:UserInfo.getUserId()];
         //Get The DefaultwareHouse for the user
     Warehouse__c[] w =[select id,name from Warehouse__c where name =: u.Formula_Warehouse__c];
         // Condition to Check if Formula_Warehouse__c =null, if null Item_Warehouse__c on page is NULL
     if(u.Formula_Warehouse__c ==null){
        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
        LitOrd.Invoice_Number__c = parOrd.id;
        LitOrd.Quantity__c=1;
       
        ords.add(LitOrd);
        }
        // Condition to Check if Formula_Warehouse__c !=null, if ! null Item_Warehouse__c on page is warehouse id(only ids display names in Lookups)
                
        else if(u.Formula_Warehouse__c !=null){
        
        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
        LitOrd.Invoice_Number__c = parOrd.id;
        LitOrd.quantity__c=1;
        LitOrd.Item_Warehouse__c =w[0].id;
        ords.add(LitOrd);
        
        }   
           
        }  
           
            
    public void removerow(){
        Integer i = ords.size();
        ords.remove(i-1);}
            
    public PageReference save() {
        insert ords;
       // PageReference home = new PageReference('/home/home.jsp');
        return(new ApexPages.StandardController(parOrd)).view();
       // home.setRedirect(true);
       // return home;
         }
        
        }

 

 

 

Thanks and Regards

 

Arun

kritinkritin

@IsTest

Private class DEV_MULTIPLE_InvoiceLineEntry()
{

 private static testmethod void TestController(){

 Test.startTest();

 Invoice__c Inv=new Invoice__c();
 Inv.Name='Test';
 insert Inv;

 Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();
     
        LitOrd.Invoice_Number__c = Inv.id;
        LitOrd.quantity__c=1;
 insert LitOrd;


 DEV_MULTIPLE_InvoiceLineEntry Control=new DEV_MULTIPLE_InvoiceLineEntry(new ApexPages.StandardController(new Invoice__c()));
    
  

 Control.addrow();
 Control.save();
 Test.stopTest();

 }


}

Jon Mountjoy_Jon Mountjoy_

While kritin's method points the way on how to create a test that instantiates the controller and invoke methods, it's not a real test at all : it doesn't check that your code actually does what you think it should do.  ie. there are no asserts.  In addition, the startTest and endTest calls are in the wrong place.

 

I highly recommend reading An Introduction to Apex Code Test Methods

 

Jon

ArunKumar_RemmaArunKumar_Remma

Hi Jon,

 

    I tried Kirtin's test class, but it is not working.  The test case is getting failed.   So could you please suggest some other option so that the test case is successful.

 

 

 

 

Regards

 

Arun

kritinkritin

Thanks Jon , For your comment. I know very well its a template/draft code that i have provided its not actual code line.

 

I know while testing we should use system.Assert method.

 

 

 

kritinkritin

Hi Arun,

See the help link that jon provided..it will definately help you in this regard

arun kumar.ax887arun kumar.ax887

Hi Jon / Kritin,

 

     I have written the test case by looking at the sample code given under the url sent by Jon,  but I am getting errors,  so could you please guide me in writing the test case as I am new to salesforce.

 

 

 

Regards

 

 

Arun

 

ArunKumar_RemmaArunKumar_Remma

Hi  Sfdcfox,

 

     I am facing problem in writing the test case for the above mentioned code, and getting errors.  so could you please provide me a solution. 

 

 

 

Thanks and Regards

 

Arun

 

 

Jon Mountjoy_Jon Mountjoy_

Hi Arun

 

Where is your test code, and the code being tested and the errors you're getting.  It's difficult to diagnose your error without that!

 

 

Regards,
Jon