この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

カスタムコントローラーおよびコントローラー拡張のテスト

すべての Apex スクリプトなど、コントローラー拡張やカスタムコントローラーに対し、単体テストを実施する必要があります。単体テストは、コード内の特定の部分が正しく機能していることを確認するクラスメソッドです。単体テストのメソッドは引数を取らず、データベースへのデータの送信を行うこともなく、メソッド定義に testMethod キーワードのフラグが立てられます。

コントローラー拡張およびカスタムコントローラークラスの単体テストを記述するときに、テストで使用できるクエリパラメーターを設定できます。たとえば、次のカスタムコントローラーとマークアップはコントローラーメソッドの例に基づいていますが、ページの URL にクエリパラメーター ?qp=yyyy が指定されていることを要求するように拡張されています。次のテストメソッドクラスは、このページの機能を���行します。

1public class thecontroller {
2
3            private String firstName;
4            private String lastName;
5            private String company;
6            private String email;
7            private String qp;
8
9            public thecontroller() {
10                this.qp = ApexPages.currentPage().getParameters().get('qp');
11            }
12
13            public String getFirstName() {
14                  return this.firstName;
15            }
16
17            public void setFirstName(String firstName) {
18                  this.firstName = firstName;
19            }
20
21            public String getLastName() {
22                  return this.lastName;
23            }
24
25            public void setLastName(String lastName) {
26                  this.lastName = lastName;
27            }
28
29            public String getCompany() {
30                  return this.company;
31            }
32
33            public void setCompany(String company) {
34                  this.company = company;
35            }
36
37            public String getEmail() {
38                  return this.email;
39            }
40
41            public void setEmail(String email) {
42                  this.email = email;
43            }
44
45            public PageReference save() {
46                PageReference p = null;
47                
48                if (this.qp == null || !'yyyy'.equals(this.qp)) {
49                    p = Page.failure;
50                    p.getParameters().put('error', 'noParam');
51                } else {
52                    try {
53                        Lead newlead = new Lead(LastName=this.lastName, 
54                                                FirstName=this.firstName, 
55                                                Company=this.company, 
56                                                Email=this.email);
57                        insert newlead;
58                    } catch (Exception e) {
59                        p = Page.failure;
60                        p.getParameters().put('error', 'noInsert');
61                    }
62                }
63                
64                if (p == null) {
65                    p = Page.success;
66                }
67                
68                p.setRedirect(true);
69                return p;
70            }
71 }

コントローラーは、成功ページと失敗ページの 2 つのページをコールします。この例では、これらのページのテキストは重要ではありません。ただし、テキストが存在することは必要です。

次のマークアップでは、上記のコントローラーを使用しています。

1<apex:page controller="thecontroller" tabstyle="lead">
2   <apex:pageBlock>
3      <apex:form>
4         <h1>Test page for adding leads</h1>
5         <p>This is a test page for adding leads.</p>
6         <p>First name: <apex:inputText value="{!FirstName}"></apex:inputText></p>
7         <p>Last name: <apex:inputText value="{!LastName}"></apex:inputText></p>
8         <p>Company: <apex:inputText value="{!Company}"></apex:inputText></p>
9         <p>Email address: <apex:inputText value="{!Email}"></apex:inputText></p>
10         <apex:commandButton action="{!save}" value="Save New Lead"/>
11      </apex:form>
12   </apex:pageBlock>
13</apex:page>

次のクラスは、コントローラーをテストします。

1@isTest
2public class thecontrollerTests {
3
4    public static testMethod void testMyController() {
5        PageReference pageRef = Page.success;
6        Test.setCurrentPage(pageRef);
7      
8        thecontroller controller = new thecontroller();
9        String nextPage = controller.save().getUrl();
10
11        // Verify that page fails without parameters
12        System.assertEquals('/apex/failure?error=noParam', nextPage);
13
14        // Add parameters to page URL
15        ApexPages.currentPage().getParameters().put('qp', 'yyyy');
16      
17        // Instantiate a new controller with all parameters in the page
18        controller = new thecontroller(); 
19        controller.setLastName('lastname');
20        controller.setFirstName('firstname');
21        controller.setCompany('acme');
22        controller.setEmail('firstlast@acme.com');
23        nextPage = controller.save().getUrl();
24
25        // Verify that the success page displays
26        System.assertEquals('/apex/success', nextPage);
27        Lead[] leads = [select id, email from lead where Company = 'acme'];
28        System.assertEquals('firstlast@acme.com', leads[0].email);
29    }
30}

コントローラーをテストするときに、次のエラーメッセージが表示される場合があります。

1Method does not exist or incorrect signature: Test.setCurrentPage(System.PageReference)

ヒント