function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sihmeieossihmeieos 

Apexスケジューラのテスト方法

こんにちは。いつもお世話になっております。

 

今回、Apexクラスのスケジューリングを行うため、Apex開発者ガイド日本語版(Beta版)[http://wiki.developerforce.com/index.php/JP:App_Logic]を参考に作成してみました。

 

[スケジュール実行クラス]

 

global class ScheduledShare implements Schedulable {
	public static String CRON_EXP = '0 0 18 ? * 2';
	global void execute(SchedulableContext ctx) {
		CreateAutoShare cas = new CreateAutoShare();
		CronTrigger ct = [select Id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where Id = :ctx.getTriggerId()];
        System.assertEquals(0, ct.TimesTriggered);
	}
}

 

[テストクラス]

 

@isTest
private class testForScheduled {
    static testMethod void testScheduledShare() {
    	ScheduledShare ss = new ScheduledShare();
    	String jobId = System.schedule('testBasicScheduledApex', ScheduledShare.CRON_EXP, ss); // Get the information from the CronTrigger API object
        CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; // Verify the expressions are the same
        System.assertEquals(ScheduledShare.CRON_EXP, ct.CronExpression); // Verify the job has not run
        System.assertEquals(0, ct.TimesTriggered);// Verify the next time the job will run 
        System.assertEquals('2010-09-27 18:00:00', String.valueOf(ct.NextFireTime));
    }
}

 

 

Eclipse上でRunTestを実行すると、スケジュール実行クラス:ScheduledShareのexecute()メソッドがコールされず、カバレッジが75%以下になってしまいます。

開発者ガイドの通りにやっているつもりなのですが、何か間違えているところがあるのでしょうか?

 

 

 

sihmeieos

tajimatajima

 

残念ながら、そのサンプルはカバー率75%を満たすようにはできていません。
execute()をテストすることはできません。
execute()の中で行っている処理を別クラスに切り出してそちらをテストする等の工夫をして、
カバー率75%を確保するようにしてください。
Apexコードのカバー率は、組織全体で計算され、個々のクラスすべてが75%以上を満たす必要は
必ずしも無いことも頭に入れておいて良いかと思います。

残念ながら、そのサンプルはカバー率75%を満たすようにはできていません。execute()をテストすることはできません。execute()の中で行っている処理を別クラスに切り出してそちらをテストする等の工夫をして、カバー率75%を確保するようにしてください。Apexコードのカバー率は、組織全体で計算され、個々のクラスすべてが75%以上を満たす必要は必ずしも無いことも頭に入れておいて良いかと思います。