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

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

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

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

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

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

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

1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="thecontroller" tabstyle="lead">
18   <apex:pageBlock>
19      <apex:form>
20         <h1>Test page for adding leads</h1>
21         <p>This is a test page for adding leads.</p>
22         <p>First name: <apex:inputText value="{!FirstName}"></apex:inputText></p>
23         <p>Last name: <apex:inputText value="{!LastName}"></apex:inputText></p>
24         <p>Company: <apex:inputText value="{!Company}"></apex:inputText></p>
25         <p>Email address: <apex:inputText value="{!Email}"></apex:inputText></p>
26         <apex:commandButton action="{!save}" value="Save New Lead"/>
27      </apex:form>
28   </apex:pageBlock>
29</apex:page>

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

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

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

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

ヒント