You need to sign in to do that
Don't have an account?
Writing the test class
Hi everyone.....
i am new to the apex Test class..so plz help me on for writing the test class for below code
trigger autonumber1 on Account (before insert)
{
for(account a:trigger.new)
{
List<account> ac=[select autonumber__c from account];
if(ac[0].autonumber__c!=null)
{
a.autonumber__c=ac[0].autonumber__c+1;
}
else
a.autonumber__c=1200;
}
}
i am getting 60% code coverage but i got the failure message like...
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, autonumber1: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.autonumber1: line 7, column 1: []
thanks in advance...
So you got the 'list index out of bounds: 0' when running the test class?
One thing could be that you have not created any test accounts to query. By default test classes no longer can query the org data. You have to create it the test data yourself.
In this case I would recommend this. In other cases if you need to query org data you can do that using the notation @isTest (SeeAllData=true)
HTH
// Niklas
All Answers
So you got the 'list index out of bounds: 0' when running the test class?
One thing could be that you have not created any test accounts to query. By default test classes no longer can query the org data. You have to create it the test data yourself.
In this case I would recommend this. In other cases if you need to query org data you can do that using the notation @isTest (SeeAllData=true)
HTH
// Niklas
Put @seeAllData and put a check before you operate on a list as
if(ac !=null and ac.size()>0)
because it may be possible that your query doesnt give any results even after @seeAllData . better handle it :)
Hi,
Not entirely sure what you are trying to accomplish with your code since your query for the List does not have a WHERE clause. Also, you will probably run into a SOQL governor limit error since you have a SOQL query inside your for loop.
I am going take a blind stab, try the following test class:
thax for your reply.
but it's not working....