+ Start a Discussion
iyappan kandasamy 4iyappan kandasamy 4 

Apex test class for fetch records from custom object

Hi,
one more doubt I have 
For fetching custom object records I have written Apex code as:

Apex class for fetching Bank object custom records
-------------------------------------------------------------------
public class RetBank
{
public void retrievedata()
{
    banks__c[] bk=[select Id,Name from banks__c];
    for(banks__c bnk:bk)
    {
        system.debug('The records are:'+bnk);
    }
}
}

Test class
-------------
@isTest
public class RetBanktest 
{
static testmethod void banks()
{
    RetBank fb=new RetBank();
    fb.retrievedata();
}
}


It is WOrking properly but the test class is giving only 75% ..Y it is not giving 100%....Any guidance please....THanks in advance
Sampath SuranjiSampath Suranji
Hi,
You need to create test data for banks__c like below,
banks__c objBank = new banks__c(name='test bank');//if you have any other required field for banks__c, then provide values to those also.
insert objBank ;
//then call the method
​ RetBank fb=new RetBank();
 fb.retrievedata();

best regards
iyappan kandasamy 4iyappan kandasamy 4
hi,
My code is only for fetching the data from the custom object ...then why i have to insert the record....Please guidance...thanks
Sampath SuranjiSampath Suranji
Hi,
In your apex class method you are quering through banks__c object.
By default pre-existing data cannot be access inside test classes(can be accessed using @isTest(SeeAllData=true) notation).
when executing the test method it is getting emty list for banks__c[] bk
In that case you have to create sample data inside the test class.
Salesforce records that are created in test methods aren’t committed to the database. They’re rolled back when the test finishes execution.

Regards
 
iyappan kandasamy 4iyappan kandasamy 4
Great Sir...Thanks a lot ...It worked out...Thanks for your explanation....