You need to sign in to do that
Don't have an account?

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:
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 SObject | Class.DEV_MULTIPLE_InvoiceLineEntry.<init>: line 13, column 21 Class.MyTestClass1.TestController: line 12, column 42 External entry point |
Thanks and Regards
Arun
Hi Arun,
I hope the error occurring in line is :
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.
Thanks
Ankit Arora
Blog | Facebook | Blog Pag
All Answers
Hi Arun,
I hope the error occurring in line is :
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.
Thanks
Ankit Arora
Blog | Facebook | Blog Pag
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
Hi Arun,
If it resolved your problem then please mark it as a solution :)
Thanks
Ankit Arora
Blog | Facebook | Blog Pag