+ Start a Discussion

Querying the database with test methods

Hey All,


I don't know if this is an actual problem or i'm just missing something really obvious. Anyways the issue I'm having is when I insert an object during a test method, If I query for that newly inserted object in the actual testMethod it returns the object. However, If I insert an object, then call a method that query's for that object it returns no results. It is kind of confusing to explain so I put some example code below. Any help would be greatly appreciated.

static testMethod void testCampMon() {     
        Client_ID__c newClient = new Client_ID__c(
                                                    Name = 'Test Client',
                                                    Client_ID__c = 'XXXXXXXXXXXXXXXXXXXXXXXX');
        insert newClient;     
        // This will return the object
	Client_ID__c c = [select Name, Client_ID__c, Id from Client_ID__c limit 1];
	System.debug('C ID = ' + c.Id);
        // This just calls the same query above (without the limit)
        // However no records are returned
        Client_ID__c client2 = CampaignMonitorService.getClientIds()[0];

// Example getClientIds() method
public static List<Client_ID__c> getClientIds() {
        return [select Name, Client_ID__c, Id from Client_ID__c];




So the test is failing with an Index Out of Bounds error?


Exactly... any ideas?