以下のテスト方法を参照してください。 Java @isTest static void testAccountUpdate() { Account acct = new Account(Name = 'Test'); acct.Integration_Updated__c = false; insert acct; CalloutUtil.sendAccountUpdate(acct.Id); Account acctAfter = [SELECT Id, Integration_Updated__c FROM Account WHERE Id = :acct.Id][0]; System.assert(true, acctAfter.Integration_Updated__c); } テストメソッドは、アカウント情報を使用して外部システムを更新するWebサービスを呼び出し、完了時にアカウントのIntegration_Updated__cチェックボックスをTrueに設定します。テストは実行に失敗し、「TestMethodとして定義されたメソッドはWebサービスの呼び出しをサポートしていません」というエラーで終了します。この問題を解決する最適な方法は何ですか?
正解:D
Salesforce enforces a strict restriction: Actual network callouts are prohibited during unit tests. This is to ensure that tests are deterministic, fast, and do not rely on the availability or state of external third-party systems. When the testing engine encounters a System.Http.send() or a web service call without a mock, it throws the error: "Methods defined as TestMethod do no1t support Web servi2ce callouts."34 To resolve this, the developer must provide a Mock Implementation. By using Test.setMock() (Option D), the developer instructs the Apex runtime to intercept any callouts and return a pre-defined response instead of attempting a real connection. The mock class5 must implement either the HttpCalloutMock interface (for REST) or the WebServiceMock interface (for SOAP). Furthermore, the call to the mock and the callout method should be wrapped in Test.startTest() and Test. stopTest().6 * Test.startTest(): Resets governor limits, providing a fresh context for the specific logic being tested.7 * Test.stopTest(): Forces any asynchronous processing (often used in cal8louts, such as @future or Queueable) to complete before the next line of code executes. In the provided code, Test.setMock must be called before CalloutUtil.sendAccountUpdate for the platform to know which mock to use. Once Test.stopTest() is reached, the mock response is processed, the checkbox is updated, and the subsequent SOQL query and assertion will correctly see the updated data. Option A is a poor practice because it skips the logic entirely, resulting in 0% code coverage for the integration logic.