SP Testing and Quality Assurance 2 — Questions and Answers
Question 1: Which Salesforce tool allows developers to run Apex tests directly from the command line as part of a CI/CD pipeline?
- Salesforce DX CLI (sf apex run test) (Correct answer)
- Workbench REST Explorer
- Developer Console Run Tests
- Setup > Apex Classes > Run All Tests
Correct answer: Salesforce DX CLI (sf apex run test)
The Salesforce CLI command `sf apex run test` enables automated test execution in CI/CD pipelines without a UI.
Question 2: What does the `@testSetup` annotation do in an Apex test class?
- Creates test data once before all test methods run, rolling back after each method (Correct answer)
- Runs the annotated method after each test method to clean up data
- Marks the method as the only test that runs in the class
- Disables test isolation so data persists across test methods
Correct answer: Creates test data once before all test methods run, rolling back after each method
`@testSetup` runs once to create shared test data that is rolled back and recreated for each individual test method, improving performance.
Question 3: A developer wants to confirm that a trigger correctly prevents duplicate Account records. Which assertion is most appropriate?
- System.assertEquals(0, [SELECT COUNT() FROM Account WHERE Name = :testName])
- System.assert(false, 'Expected exception not thrown')
- Try/catch block catching DmlException and asserting the error message (Correct answer)
- System.assertNotEquals(null, newAccount.Id)
Correct answer: Try/catch block catching DmlException and asserting the error message
Using a try/catch block to catch the DmlException and asserting the error message confirms the trigger throws the correct exception for duplicates.
Question 4: What is the recommended approach when a test method needs to call an external web service?
- Use Test.setMock() with an HttpCalloutMock implementation (Correct answer)
- Enable 'Allow Callouts in Tests' in Setup
- Call the endpoint directly since sandboxes have internet access
- Wrap the callout in a future method to bypass restrictions
Correct answer: Use Test.setMock() with an HttpCalloutMock implementation
`Test.setMock()` with `HttpCalloutMock` provides a simulated HTTP response so tests can validate callout logic without real network calls.
Question 5: Which statement best describes a unit test in the context of Salesforce Apex development?
- A test that validates a single method or small unit of code in isolation from dependencies (Correct answer)
- A test that validates end-to-end business processes across multiple objects
- A test executed by QA engineers using Selenium against a full sandbox
- A test that verifies integration between Salesforce and an external system
Correct answer: A test that validates a single method or small unit of code in isolation from dependencies
Unit tests focus on a single, isolated unit of code (method or class) and use mocks or stubs to remove external dependencies.
Question 6: When should `Test.startTest()` and `Test.stopTest()` be used in a test method?
- To reset governor limits and force asynchronous jobs to complete synchronously (Correct answer)
- To prevent test data from being committed to the database
- To enable callouts within the test execution block
- To suppress debug logs during the test run
Correct answer: To reset governor limits and force asynchronous jobs to complete synchronously
`Test.startTest()` resets governor limits for the test execution block, and `Test.stopTest()` forces async operations like future methods to complete before assertions.
Question 7: A change set deployment fails because code coverage drops below the required threshold. What is the minimum coverage percentage required for production deployments?
- 75% (Correct answer)
- 50%
- 80%
- 100%
Correct answer: 75%
Salesforce requires at least 75% overall Apex code coverage for production deployments, and every trigger must have at least 1% coverage.
Which Salesforce tool allows developers to run Apex tests directly from the command line as part of a CI/CD pipeline?