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
DanSadDanSad 

Why are my unit tests failing?

I wrote a class with a few functions that return account IDs based on a custom field.

 

public class ComplaintOperations{
    public static Id getaid(string client_id){
        //Try to find Account with matching client_id, with or without leading 0s. return Account ID if found, otherwise return null
        Account[] a = [find :client_id in all fields returning Account(Id,client_id__c where Client_Id__c like :client_id) limit 1][0];
        if (a.size() !=0){    
        	return a[0].id;}
        else{string othercid = '00'+client_id;
        	Account[] a2 = [find :othercid in all fields returning Account(Id,client_id__c where Client_Id__c like :othercid) limit 1][0];
            if(a2.size()!= 0){return a2[0].id;}
             else{return null;}
            }
    }
             
             
    public static Boolean isclientid(string client_id){
        if(getaid(client_id) == null){return false;}
        else{return true;}
    }
}

 It works just fine when i run it from the console:

system.debug(complaintoperations.getaid('0012345'));

 yields the correct ID.

 

But my unit tests:

@isTest
public class testComplaintOperations{
    
    static testMethod void testGetAid(){
        string cid = '999999999';
        Account a = New Account(Name='Daniel Sadster',client_id__c = cid,status__c = 'Active');
        insert a;
        system.assert(a != null);
        system.assertequals(a.id,ComplaintOperations.getaid(cid));
        System.assertequals(null,ComplaintOperations.getaid('123'));
    }
    static testMEthod void testisclient(){
    	string cid = '999999999';
        Account a = New Account(Name='Daniel Sadster',client_id__c = cid,status__c = 'Active');
        insert a;
        system.assertequals(Complaintoperations.isclientid(cid),true);
        system.assertequals(Complaintoperations.isclientid('pooppypants'),false);
    }
}

 Keep failing!

Apparently because the "find" statements in my class yield nothing.

Any ideas?

 

Best Answer chosen by Admin (Salesforce Developers) 
gaisergaiser

In order to test SOSL query with non empty result you have to use

Test.setFixedSearchResults(fixedSearchResults);

More detailed explanation here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm

 

All Answers

gaisergaiser

In order to test SOSL query with non empty result you have to use

Test.setFixedSearchResults(fixedSearchResults);

More detailed explanation here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm

 

This was selected as the best answer
DanSadDanSad
Thanks so much!