Salesforce-Platform-Developer-II Practice Test

Salesforce Spring 25 Release
202 Questions

Consider the following code snippet:

However, when the test runs, no data is returned and the assertion fails. Which edit should the developer make to ensure the test class runs successfully?

A. Implement the without sharing keyword in the searchFeaturs Apex class.

B. Implement the seeAllData=true attribute in the @1sTest annotation.

C. Enclose the method call within Test. startbest i) and @Test_stopTast |).

D. Implement the setFixedSearchResult= method in the test class.

D.   Implement the setFixedSearchResult= method in the test class.

Explanation:

Understanding the Problem
You’re dealing with a failing test class in Salesforce Apex where a SOSL search isn’t returning data, causing an assertion to fail. The test checks a method called searchRecords in a class named searchFeature. This method uses SOSL to search for a query term ('Test') across all fields of Account, Opportunity, and Lead objects, returning a List>—a list containing one list per object type. The test method, searchRecords_Test, calls this and asserts that records.size() is not zero. However, the test fails because no data is returned. There’s also an empty @TestSetup method, meaning no test data is created. Let’s explore why this happens and how to fix it by evaluating four options.

Why the Test Fails
In Apex, test methods run in isolation, meaning they don’t see an organization’s real data unless explicitly allowed (e.g., with seeAllData=true). Since the @TestSetup method is empty, no test records exist when the SOSL query runs. Normally, a SOSL query like FIND 'Test' RETURNING Account, Opportunity, Lead returns a list with three inner lists—one for each object type—each empty if no matches are found. So, records.size() should be 3 (the number of object types), and the assertion System.assertNotEquals(records.size(), 0) should pass, since 3 ≠ 0.

But the problem states the assertion fails, suggesting records.size() is 0 or something else is wrong. In test contexts, SOSL behavior can differ. Without test data or specific configuration, SOSL might not behave as expected. Salesforce documentation notes that SOSL in tests returns no results unless you use Test.setFixedSearchResults() to mock the results. If no fixed results are set, it may return an empty list ([]) rather than a list of empty lists ([[], [], []]), making records.size() 0, which fails the assertion.

Alternatively, the test might intend to verify that actual records are found (inner lists aren’t empty), but the assertion only checks records.size(). Since the problem specifies the assertion is System.assertNotEquals(records.size(), 0) and it’s failing, the most likely issue is that SOSL returns no data in the test context without proper setup.

Evaluating the Options

Let’s examine the four options to find the best fix:

Option A: Implement the without sharing keyword in the searchFeature class
What it does: The without sharing keyword makes the class ignore user sharing rules, running in system context.
Impact: This affects visibility of existing records based on permissions. However, in a test with no data (due to an empty @TestSetup), there are no records to see, regardless of sharing. This won’t create data or make SOSL return results.
Conclusion: This doesn’t solve the problem.

Option B: Implement the seeAllData=true attribute in the @isTest annotation
What it does: Adding @isTest(seeAllData=true) lets the test access all data in the Salesforce org.
Impact: If the org has Account, Opportunity, or Lead records with 'Test' in any field, the SOSL query might find them, populating records with non-empty inner lists. Since records.size() would still be 3 (one list per object type), the assertion could pass if data exists. However, this relies on unpredictable org data, making the test brittle and non-portable—bad practice in Apex testing.
Conclusion: This works only if suitable data exists, but it’s unreliable and discouraged.

Option C: Enclose the method call within Test.startTest() and Test.stopTest()
What it does: These methods mark the test’s main execution block, resetting governor limits and ensuring asynchronous code completes before assertions.
Impact: This is good practice for testing, especially with asynchronous operations, but it doesn’t provide data or alter SOSL behavior. With no test records, the SOSL query still finds nothing.
Conclusion: This doesn’t address the data issue.

Option D: Implement the setFixedSearchResults method in the test class
What it does: Test.setFixedSearchResults() lets you define which records SOSL returns in a test by passing an array of record IDs.
Impact: You can create test records (e.g., an Account, Opportunity, and Lead with 'Test' in their fields), insert them, and pass their IDs to setFixedSearchResults(). When searchRecords('Test') runs, SOSL returns these records, ensuring records has three inner lists, some non-empty. Since records.size() is 3, the assertion passes. This keeps the test self-contained and reliable, aligning with Apex best practices.
Conclusion: This directly fixes the issue by providing controlled test data.

Choosing the Best Solution
Option A fails because it doesn’t create data.
Option B might work but depends on org data, which is unreliable and against testing principles.
Option C is irrelevant to the data problem.
Option D ensures the SOSL query returns results by mocking them, making the test pass consistently without external dependencies.

Option D is the clear winner. It leverages Test.setFixedSearchResults(), a standard tool for testing SOSL in Apex, ensuring the test controls what SOSL returns. You’d typically:
Create test records in the test method or @TestSetup.
Set their IDs as fixed search results.
Run the SOSL query and assert the results.

This approach confirms records.size() is 3 and allows further checks (e.g., inner list contents), though the given assertion only requires a non-zero size.

Final Recommendation
The best way to fix the failing test is to implement the setFixedSearchResults method in the test class (Option D). This ensures the SOSL query returns predefined records, making the assertion System.assertNotEquals(records.size(), 0) pass reliably in the isolated test environment.

Salesforce-Platform-Developer-II Practice-Test - Home Previous
Page 12 out of 202 Pages