The@TestVisibleannotation allows private methods or variables to be accessed in test classes. This ensures that the private method can be adequately tested without changing its access modifier. Reference:Testing Apex Code Below is the formatted response for the provided question, adhering to the specified format and requirements. The question falls under the Salesforce Platform and Declarative Features topic, as it involves securing SOQL queries against injection vulnerabilities in the context of Visualforce, which is a key concept in the Salesforce Platform Developer I certification. The answer is based on official Salesforce Platform Developer I documentation, with a comprehensive explanation and references to relevant Salesforce documentation. Since the question asks for two correct answers, the response will identify both and explain why they are safe, as well as why the others are not.
最新のコメント (最新のコメントはトップにあります。)
正解は **D** です。
### 正解の解説
**D. @TestVisible アノテーションを使用します。**
これが正解です。`@TestVisible` アノテーションを使用すると、プライベート(`private`)または保護された(`protected`)メンバー(メソッド、変数、内部クラス)を、テストクラスからアクセス可能にすることができます。これにより、アクセス修飾子を `public` や `global` に変更することなく、カプセル化を維持したまま、内部ロジックの単体テストを行うことが可能になります。
**使用例:**
```apex
public class MyClass {
// 本来は外部から見えないプライベートメソッド
@TestVisible
private static Integer calculateSomething(Integer x) {
return x * 2;
}
}
@isTest
private class MyClassTest {
@isTest
static void testCalculate() {
// @TestVisibleがあるため、テストクラスから呼び出し可能
Integer result = MyClass.calculateSomething(5);
System.assertEquals(10, result);
}
}
```
### 不正解の解説
**A. SeeAllData アノテーションを使用します。**
これは不正解です。`@isTest(SeeAllData=true)` は、テストメソッドが組織内の実際のデータ(レコード)にアクセスできるようにするためのアノテーションです。コードの可視性(プライベートメソッドへのアクセス権)とは無関係です。
**B. Apex クラスにテスト メソッドを追加します。**
これは不正解です。現在のApex開発のベストプラクティスでは、テストメソッドはメインのクラス内ではなく、別のテストクラス(`@isTest` アノテーションが付いたクラス)に記述すべきです。また、仮に同じクラス内に書いたとしても、それはメソッドの可視性を制御する機能ではありません。
**C. Apex クラスをグローバルとしてマークします。**
これは不正解です。クラスを `global` にしても、その中の `private` メソッドが自動的に公開されるわけではありません。また、テストのためだけにクラスやメソッドのアクセスレベルを広げる(`private` から `public` や `global` に変える)ことは、セキュリティや設計の観点から推奨されません。...