Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/aadcae086d8dfc94c150 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/aadcae086d8dfc94c150 to your computer and use it in GitHub Desktop.
Queueable インターフェースの実装サンプルです。(テストクラスでエラーでてます。。)
<apex:page controller="QueueableDemoController" showHeader="true" sidebar="false">
<apex:form id="form">
<apex:pageBlock title="Queueable Demo">
<apex:commandButton value="Execute!!" action="{!doExecute}" reRender="form" />
&nbsp;
<apex:commandButton value="Refresh!!" action="{!doRefresh}" reRender="form" />
</apex:pageBlock>
<apex:pageBlock title="{!$ObjectType.AsyncApexJob.Label}" rendered="{!jobInfos.size > 0}">
<apex:pageBlockTable value="{!jobInfos}" var="item">
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.Id.Label}">
<apex:outputField value="{!item.Id}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.Status.Label}">
<apex:outputField value="{!item.Status}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.NumberOfErrors.Label}">
<apex:outputField value="{!item.NumberOfErrors}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class QueueableDemoController {
public List<AsyncApexJob> jobInfos {get; set;}
private Id jobID = null;
public QueueableDemoController() {
this.jobInfos = new List<AsyncApexJob>();
}
public void doExecute() {
this.jobID = System.enqueueJob(new AsyncExecutionExample());
if (String.isNotEmpty(this.jobID)) {
this.jobInfos = getAsyncApexJob(this.jobID);
}
}
public void doRefresh() {
if (String.isNotEmpty(this.jobID)) {
this.jobInfos = getAsyncApexJob(this.jobID);
}
}
private List<AsyncApexJob> getAsyncApexJob(Id jobID) {
return [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
}
}
@isTest
private class QueueableDemoControllerTest {
static testMethod void QueueableDemoControllerTest() {
Test.startTest();
QueueableDemoController cls = new QueueableDemoController();
cls.doExecute();
System.assertEquals(cls.jobInfos.isEmpty(), true);
Test.stopTest();
cls.doRefresh();
System.assertEquals(cls.jobInfos.isEmpty(), false);
}
}
public with sharing class AsyncExecutionExample implements Queueable {
public void execute(QueueableContext context) {
Account a = new Account(Name='Acme',Phone='(415) 555-1212');
insert a;
}
}
@isTest
public class AsyncExecutionExampleTest {
static testmethod void test1() {
// startTest/stopTest block to force async processes
// to run in the test.
Test.startTest();
ID jobID = System.enqueueJob(new AsyncExecutionExample());
Test.stopTest();
// Validate that the job has run
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
System.assertEquals('Completed', jobInfo.Status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment