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

Newer Version Available

This content describes an older version of this product. View Latest

テストの例

単純なマイル追跡アプリケーションでテストを実行します。アプリケーションの既存のコードは、1 日に入力されるマイル数が 500 マイルを超えないことを確認します。主オブジェクトは Mileage__c というカスタムオブジェクトです。全体のテストクラスを次に示します。次のセクションでは、コードの特定の部分の手順を説明します。

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class MileageTrackerTestSuite {
19
20    static testMethod void runPositiveTestCases() {
21        
22        Double totalMiles = 0;
23        final Double maxtotalMiles = 500;
24        final Double singletotalMiles = 300;
25        final Double u2Miles = 100;
26  
27        
28        //Set up user
29        User u1 = [SELECT Id FROM User WHERE Alias='auser'];
30        
31        //Run As U1
32        System.RunAs(u1){
33
34            
35        System.debug('Inserting 300  miles... (single record validation)');
36        
37        Mileage__c testMiles1 = new Mileage__c(Miles__c = 300, Date__c = System.today());
38        insert testMiles1;
39        
40        //Validate single insert
41        for(Mileage__c m:[SELECT miles__c FROM Mileage__c 
42            WHERE CreatedDate = TODAY
43            and CreatedById = :u1.id
44            and miles__c != null]) {
45                totalMiles += m.miles__c;
46            }
47        
48        System.assertEquals(singletotalMiles, totalMiles);
49    
50    
51        //Bulk validation   
52        totalMiles = 0; 
53        System.debug('Inserting 200 mileage records... (bulk validation)');
54        
55        List<Mileage__c> testMiles2 = new List<Mileage__c>();
56        for(integer i=0; i<200; i++) {
57            testMiles2.add( new Mileage__c(Miles__c = 1, Date__c = System.today()) );
58        }
59        insert testMiles2;
60       
61        for(Mileage__c m:[SELECT miles__c FROM Mileage__c
62            WHERE CreatedDate = TODAY
63            and CreatedById = :u1.Id
64            and miles__c != null]) {
65                totalMiles += m.miles__c;
66            }
67        
68        System.assertEquals(maxtotalMiles, totalMiles);
69
70        }//end RunAs(u1)
71
72
73       //Validate additional user:
74       totalMiles = 0;
75       //Setup RunAs
76       User u2 = [SELECT Id FROM User WHERE Alias='tuser'];
77       System.RunAs(u2){
78        
79        Mileage__c testMiles3 = new Mileage__c(Miles__c = 100, Date__c = System.today());
80        insert testMiles3;
81        
82                for(Mileage__c m:[SELECT miles__c FROM Mileage__c
83            WHERE CreatedDate = TODAY
84            and CreatedById = :u2.Id
85            and miles__c != null]) {
86                totalMiles += m.miles__c;
87            }
88        //Validate 
89        System.assertEquals(u2Miles, totalMiles);
90        
91       } //System.RunAs(u2)
92
93      
94    } // runPositiveTestCases()
95   
96    static testMethod void runNegativeTestCases() {
97
98       User u3 = [SELECT Id FROM User WHERE Alias='tuser'];
99       System.RunAs(u3){
100        
101       System.debug('Inserting a record with 501 miles... (negative test case)');
102        
103       Mileage__c testMiles3 = new Mileage__c( Miles__c = 501, Date__c = System.today() );
104        
105        try {
106            insert testMiles3;
107        } catch (DmlException e) {
108            //Assert Error Message
109            System.assert( e.getMessage().contains('Insert failed. First exception on ' +
110                'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
111                'Mileage request exceeds daily limit(500): [Miles__c]'), 
112                e.getMessage() );
113                  
114            //Assert field
115            System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);
116            
117            //Assert Status Code
118            System.assertEquals('FIELD_CUSTOM_VALIDATION_EXCEPTION' , 
119                                 e.getDmlStatusCode(0) );
120        } //catch
121       } //RunAs(u3) 
122    } // runNegativeTestCases() 
123  
124    
125} // class MileageTrackerTestSuite

ポジティブテストケース

上記のコードの、単一レコードおよび複数レコードのポジティブテストケースの手順は、次のとおりです。

  1. デバッグログにテキストを追加して、コードの次のステップを示します。
    1System.debug('Inserting 300 more miles...single record validation');
  2. Mileage__c オブジェクトを作成し、データベースに挿入します。
    1Mileage__c testMiles1 = new Mileage__c(Miles__c = 300, Date__c = System.today() );
    2insert testMiles1;
  3. 挿入されたレコードを返してコードを検証します。
    1for(Mileage__c m:[SELECT miles__c FROM Mileage__c 
    2   WHERE CreatedDate = TODAY 
    3   and CreatedById = :createdbyId 
    4   and miles__c != null]) {
    5       totalMiles += m.miles__c;
    6    }
  4. system.assertEquals メソッドを使用して、期待どおりの結果が返されたことを確認します。
    1System.assertEquals(singletotalMiles, totalMiles);
  5. 次のテストに移る前に、合計マイル数を 0 に再設定します。
    1totalMiles = 0;
  6. 200 レコードの一括挿入を作成して、コードを検証します。
    まず、デバッグログにテキストを追加し、コードの次のステップを示します。
    1System.debug('Inserting 200 Mileage records...bulk validation');
  7. 次に 200 件の Mileage__c レコードを挿入します。
    1List<Mileage__c> testMiles2 = new List<Mileage__c>();
    2for(Integer i=0; i<200; i++){
    3testMiles2.add( new Mileage__c(Miles__c = 1, Date__c = System.today()) );
    4   }
    5insert testMiles2;
  8. System.assertEquals を使用して、期待どおりの結果が返されたことを確認します。
    1for(Mileage__c m:[SELECT miles__c FROM Mileage__c 
    2   WHERE CreatedDate = TODAY 
    3   and CreatedById = :CreatedbyId 
    4   and miles__c != null]) {
    5       totalMiles += m.miles__c;
    6    }
    7       System.assertEquals(maxtotalMiles, totalMiles);

ネガティブテストケース

上記のコードのネガティブテストケースの手順は、次のとおりです。

  1. runNegativeTestCases という静的テストメソッドを作成します。
    1static testMethod void runNegativeTestCases(){
  2. デバッグログにテキストを追加して、コードの次のステップを示します。
    1System.debug('Inserting 501 miles... negative test case');
  3. 501 マイルの Mileage__c レコードを作成します。
    1Mileage__c testMiles3 = new Mileage__c(Miles__c = 501, Date__c = System.today());
  4. insert ステートメントを try/catch ブロック内に配置します。これで、検証の例外を捕捉し、生成されたエラーメッセージを表示できます。
    1try {
    2     insert testMiles3;
    3     } catch (DmlException e) {
  5. System.assert および System.assertEquals を使用してテストを実行します。次のコードを、前に作成した catch ブロックに追加します。
    1//Assert Error Message
    2   System.assert(e.getMessage().contains('Insert failed. First exception '+
    3      'on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, '+
    4      'Mileage request exceeds daily limit(500): [Miles__c]'), 
    5         e.getMessage()); 
    6
    7//Assert Field
    8   System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);
    9
    10//Assert Status Code
    11   System.assertEquals('FIELD_CUSTOM_VALIDATION_EXCEPTION'  , 
    12                        e.getDmlStatusCode(0));
    13          }
    14   }
    15}

セカンドユーザとしてのテスト

上記のコードを、セカンドユーザとして実行する手順は次のとおりです。

  1. 次のテストに移る前に、合計マイル数を 0 に再設定します。
    1totalMiles = 0;
  2. 次のユーザを設定します。
    1User u2 = [SELECT Id FROM User WHERE Alias='tuser'];
    2       System.RunAs(u2){
  3. デバッグログにテキストを追加して、コードの次のステップを示します。
    1System.debug('Setting up testing - deleting any mileage records for ' +
    2            UserInfo.getUserName() +
    3            ' from today');
  4. 次に 1 件の Mileage__c レコードを挿入します。
    1Mileage__c testMiles3 = new Mileage__c(Miles__c = 100, Date__c = System.today());
    2insert testMiles3;
  5. 挿入されたレコードを返してコードを検証します。
    1for(Mileage__c m:[SELECT miles__c FROM Mileage__c 
    2   WHERE CreatedDate = TODAY 
    3   and CreatedById = :u2.Id 
    4   and miles__c != null]) {
    5       totalMiles += m.miles__c;
    6    }
  6. system.assertEquals メソッドを使用して、期待どおりの結果が返されたことを確認します。
    1System.assertEquals(u2Miles, totalMiles);