A: Thesave()method in the custom controller must be tested to ensure it works as intended and redirects to the correct page. B: Setting parameters viaApexPages.currentPage().getParameters()is crucial to test scenarios where user input or URL parameters drive logic. D: UsingTest.setCurrentPage(pageRef)is essential for simulating page context in unit tests. Why not other options? C: This statement does not belong to testing a custom controller. B (second occurrence): It appears to be incorrectly formatted in the question. Testing Apex Controllers
最新のコメント (最新のコメントはトップにあります。)
正:A,B,E
正解は **A**、**B**、**E** です。
この問題は、Visualforce のカスタムコントローラーをテストする際の「ページコンテキストの設定」と「ナビゲーションの検証」に関する理解を問うものです。
### 正解の解説
**E. `Test.setCurrentPage(pageRef);`**
* **理由:** 単体テスト環境では、ブラウザのように「現在開いているページ」という概念が自動的には存在しません。
* [cite_start]`ApexPages.currentPage()` を使用してパラメータやヘッダーを操作する前に、`Test.setCurrentPage()` を使用して、「今このページをテストしている」というコンテキスト(文脈)を明示的に設定する必要があります。これを行わないと、`currentPage()` が null を返す可能性があります [cite: 1816]。
**B. `ApexPages.currentPage().getParameters().put('input', 'TestValue');`**
* **理由:** 問題文に「初期 URL のパラメータからデータを受け入れます」とあります。
* [cite_start]テストコード内で URL パラメータをシミュレート(模倣)するには、現在のページのパラメータマップ (`getParameters()`) を取得し、そこに直接キーと値を `put` する必要があります [cite: 1817-1821]。
**A. `String nextPage = controller.save().getUrl();`**
* **理由:** ウィザード形式の画面では、ボタンを押した後の「画面遷移」が正しいかをテストすることが重要です。
* [cite_start]コントローラーのアクションメソッド(`save()` など)は通常 `PageReference` オブジェクトを返します。そのオブジェクトから `getUrl()` で遷移先の URL を取得し、期待通りのページに移動しているか(アサーション)を確認するために使用します [cite: 1815-1817]。
---
### 不正解の解説
**C. `insert pageret;`**
* これは **不正解** です。
* `PageReference` は Apex のシステムクラスであり、データベースに保存される sObject(Account や Contact など)ではありません。DML 操作(insert)を行うことはできず、コンパイルエラーになります。
**D. `public ExtendedController(ApexPages.StandardController cntrl) { }`**
* これは **不正解** です。
* [cite_start]これはテストコード内のステートメントではなく、**コントローラー拡張 (Controller Extension)** のコンストラクタの定義です [cite: 1750]。
* [cite_start]問題文では「単一のカスタムコントロー...
A,B,E??