+ Start a Discussion

SOSL in tests causing other tests to fail

I've got a VisualForce controller that uses SOSL to search for Contacts. I have tests of this controller that pass, and use the correct method for overriding SOSL results in the test:


Id[] fixedSearchResults=new Id[3];









My actual SOSL call is in a method in a different class, and everything is hunky dory when I run that test. My class correctly handles the SOSL result, even when it's null, as that's a common occurrence when searching.


When I run All Tests, however, I get failures in an unrelated test class. After some hacking apart my controller test class, it appears that the SOSL call is what is causing the unrelated test class to fail.


By commenting out the SOSL, I can get the unrelated test to pass. If I uncomment the SOSL, the unrelated test will fail.



//build the SOSL query and execute


String searchquery = 'FIND \'' + searchText + '\' IN ALL FIELDS RETURNING Contact(' + fieldsForReturn +' ORDER BY LastName LIMIT ' + SOSL_LIMIT + ')';


//if I comment this out, the unrelated test will pass


List<List<SObject>> searchList = search.query(searchquery);


The SOSL field list is generated from a describe call, but I get the same problem if I used a fixed string for the field list. 


Any ideas how SOSL might cause another test in the test batch to fail? I suspected a governor limit problem, just because I couldn't think of any other reasons why this would happen. But I don't see how that could be the case, and commenting out my bulk tests hasn't helped.




What does the other test method that is failing designed to test and what failure message(s) are you getting with it?

My other test methods that are failing test some trigger behavior when Contacts are created/edited/deleted. We are doing some automatic AccountId filling when Contacts are created.


The test failures are showing that the AccountIds aren't getting set right on the Contacts. So I get an assertion failing--the AccountId doesn't equal what I expect it to be.


That test passes in isolation and it passes in Run All Tests if my SOSL is commented out.




Here is a simplification of my problem. I have many test classes that are all running and passing. If I add this test class, save it, and Run All tests, some tests that previously passed, now fail:





private class TEST_ContactMergeController {

static testMethod void soslTest() {

//build the SOSL query and execute

String searchquery = 'FIND \'test\' IN ALL FIELDS RETURNING Contact(LastName ORDER BY LastName LIMIT 10)';


List<List<SObject>> junkSearch = search.query(searchquery);




So adding a Test class with a test method that does nothing but execute a SOSL is causing other tests in my batch to fail. This feels like a bug now. 


Any ideas?


Thanks much,