LoginSignup
0
1

More than 1 year has passed since last update.

Sfdc バッチテスト例

Last updated at Posted at 2022-12-21

ポイント

・データの準備が別にする
・データがStringで共通方法でInsert、Selectする
・テスト前後のデータを対照する
・バッチの実行は特定のキーワードで囲む

setUp

@isTest
private class AccountLinkRMListTest{
    @TestSetup static void setUp() {
        string strAcc ='['
        +'{"Name":"TST000001","corporate_number__c":"123456"},'
        +'{"Name":"TST000002"}]';

         CommonDmlUtil.commonInsertRecords(strAcc,'List<Account>',true);
         List<Account> accList = [SELECT id, name FROM Account];
         System.debug(accList); 
    }

test method
prepare data before and display it after

    @isTest 
    static void batchTestCase005(){
        //defore
        Account accList0 = [SELECT id,Name,LastModifiedDate FROM Account  WHERE corporate_number__c = '123456' limit 1];
        System.debug('Case005before 取引先'+accList0);
        
        RMBusinessSectionList__c rmList00 = [ SELECT id, Account__c,LastModifiedDate FROM RMBusinessSectionList__c
            WHERE corporate_number__c = '123456' limit 1 ];
        System.debug('Case005before RM営業 取引先 null : ' + rmList00);

        System.debug('Case005指定期間: '+System.Label.AccountLinkRMListPeriod+'');
        rmList00.Account__c=accList0.id;
        update rmList00;
        RMBusinessSectionList__c rmList01 = [ SELECT id, Account__c,LastModifiedDate FROM RMBusinessSectionList__c
            WHERE corporate_number__c = '123456' limit 1 ];
           
       
        System.debug('Case005before RM営業: ' + rmList01);
        //execute
        Test.startTest();
        AccountLinkRMList bc = new AccountLinkRMList();
        Database.executeBatch(bc, 20);
        Test.stopTest();
  
        RMBusinessSectionList__c rmList03 = [ SELECT id, Account__c,LastModifiedDate FROM RMBusinessSectionList__c
        WHERE corporate_number__c = '123456' limit 1 ];
        System.debug('Case005after RM営業: ' + rmList03);

        //対象のレコードが更新されないこと
        System.assertEquals(accList0.Id, rmList03.Account__c);
  
    
    }

補足データ処理の共通モジュール

public with sharing class CommonDmlUtil {
    /**
     * @name commonInsertRecords
     * @parameter String strJSONData 
     *   sample:[{},{},{}] or {} if signle
     * @parameter String strType
     *   sample: '<List>Contact' or 'Contact' if sigle
     * @parameter Boolean boolIsList
     *  sample: true of false if sigle
     * @return void
     */
    @AuraEnabled(cacheable=false)
    public static void commonInsertRecords(String strJSONData, String strType, Boolean boolIsList) {
        List<SObject> lstObjects = new List<SObject>();
        if (boolIsList) {
            lstObjects = (List<SObject>) JSON.deserialize(strJSONData, Type.forName(strType));
        } else {
            SObject obj = (SObject) JSON.deserialize(strJSONData, Type.forName(strType));
            lstObjects.add(obj);
        }
        System.debug('lstObject: '+lstObjects);

        insert lstObjects;
       
    }

    /**
     * @name commonSelectRecords
     * @parameter String strSql 
     *   sample: 'select id, name from Contact'
     * @return List<SObject>
     */
    @AuraEnabled(cacheable=true)
    public static List<SObject> commonSelectRecords(String strSql) {

        return Database.query(strSql);
       
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1