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
arun kumar.ax887arun kumar.ax887 

How to write Test Case for a Custom Controller

Hi All,

 

     We have written a Custom controller in order to implement Multiple Invoice Line Items.  The code is working properly, but  when I am trying to write a test case for  the above mentioned controller,  I am getting errors.  Could any one please correct the errors.  I am posting both Apex class and Test Case.  So kindly go through the code and provide a better solution.

 

 

Apex Class:

 

 

 

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];
     invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];
     
     // 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.staff__c = pa.Doctor__c;
        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.staff__c = pa.Doctor__c;
        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];
     
     invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];
         // 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.staff__c = pa.Doctor__c;
        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.staff__c = pa.Doctor__c;
        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;
         }
        
        }

 

 

 

TestCase:

 

 

 

@isTest

private class MyTestClass1

{

 

 static testmethod void TestController()

 {

 

      ApexPages.StandardController con = new ApexPages.StandardController(new Invoice__c());

 

      Invoice__c parOrd = (Invoice__c)con.getRecord();

 

      DEV_MULTIPLE_InvoiceLineEntry dc = new DEV_MULTIPLE_InvoiceLineEntry(con);

 

      List<Invoice_Line_Item__c> ords = new List<Invoice_Line_Item__c>();

 

      user u1 =[select id ,name,Formula_Warehouse__c from user where id=:UserInfo.getUserId()];

 

      Warehouse__c[] w1 =[select id,name from Warehouse__c where name =: u1.Formula_Warehouse__c or name = null];

 

      invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];

 

      if(u1.Formula_Warehouse__c ==null)

      {

        Invoice_Line_Item__c LitOrd = new Invoice_Line_Item__c();

        LitOrd.Invoice_Number__c = parOrd.id;

        LitOrd.staff__c = pa.Doctor__c;

        LitOrd.quantity__c=1;

        ords.add(LitOrd);

 

        }

 

       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.staff__c = pa.Doctor__c;

        LitOrd.Item_Warehouse__c =w1[0].id;

        ords.add(LitOrd);

        } 

 

 

        dc.addrow();

        dc.removerow();

        dc.save();

 

}     

 

 }

 

 

 

I am getting the following Error Messages,  so could any one please correct them, so that the test case is successful.

 

Error Messages:

 




System.QueryException: List has no rows for assignment to SObjectClass.DEV_MULTIPLE_InvoiceLineEntry.<init>: line 13, column 21 Class.MyTestClass1.TestController: line 12, column 42 External entry point

 

 

 

 

Thanks and Regards

 

 

Arun

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Hi Arun,


I hope the error occurring in line is : 

 

 

invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];

 

 

 

When you call the class constructor from your test class then it gives error. So do one thing insert invoice record in test class and pass the record instance as standard controller.

 

 

Invoice__c standCon = new Invoice__c();
insert standCon ;ApexPages.StandardController con = new ApexPages.StandardController(standCon);
 
Invoice__c parOrd = (Invoice__c)con.getRecord();
 
DEV_MULTIPLE_InvoiceLineEntry dc = new DEV_MULTIPLE_InvoiceLineEntry(con);

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Pag

All Answers

Ankit AroraAnkit Arora

Hi Arun,


I hope the error occurring in line is : 

 

 

invoice__c pa =[select id,name, doctor__c from Invoice__c where id=:parOrd.id];

 

 

 

When you call the class constructor from your test class then it gives error. So do one thing insert invoice record in test class and pass the record instance as standard controller.

 

 

Invoice__c standCon = new Invoice__c();
insert standCon ;ApexPages.StandardController con = new ApexPages.StandardController(standCon);
 
Invoice__c parOrd = (Invoice__c)con.getRecord();
 
DEV_MULTIPLE_InvoiceLineEntry dc = new DEV_MULTIPLE_InvoiceLineEntry(con);

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Pag

This was selected as the best answer
arun kumar.ax887arun kumar.ax887

Hi Ankit,

 

     Thank you very much Ankit,  as per your suggestion I inserted your code snippet at the required location, and the TestCase is successful.  

 

     Once again thank you very much.

 

 

 

Sincere Regards

 

Arun

 

Ankit AroraAnkit Arora

Hi Arun,

 

If it resolved your problem then please mark it as a solution :)

 

Thanks

Ankit Arora

Blog | Facebook | Blog Pag