+ Start a Discussion
rohitrrohitr 

Test Class for class with AsyncApexJob

Hi,

 

I've the elow method which is called on a command button from a VF page. It is for the admin to run the batch manually whenever required.

 

Please help me how to create a test class for the same.

i tried creating the test class by created an AsyncApexJob record, but it gives error saying that the fields are not writeable.

 

public List<AsyncApexJob> getBatchJob() {
        String kstCreatedDateMod;
        String kstCompletedDateMod;
        List<AsyncApexJob> batchJob = [SELECT CreatedDate, JobItemsProcessed, Status, ExtendedStatus, NumberOfErrors, CompletedDate, JobType, TotalJobItems, ApexClassId FROM AsyncApexJob where id = : batchProcessId ];
       
        for(AsyncApexJob aaj : batchJob){
            if(aaj.CreatedDate == null){
                kstCreatedDate = null;
            }
            else {
                kstCreatedDateMod = String.valueOf(aaj.CreatedDate);
                kstCreatedDate = kstCreatedDateMod +' KST';     
            }
            if(aaj.CompletedDate == null){
                kstCompletedDate = null;
            }
            else {
                kstCompletedDateMod = String.valueOf(aaj.CompletedDate);
                kstCompletedDate = kstCompletedDateMod +' KST';
            }
            if(aaj.status == 'Queued') {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Job Queued... Please wait..'));
            }
            else if(aaj.Status == 'Processing') {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Batch Job Running... Please wait..'));
            }
            else if((aaj.Status == 'Completed') && (aaj.NumberOfErrors == 0)){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Batch Job Completed!'));
            }
            else if((aaj.NumberOfErrors > 0) || (aaj.Status == 'Failed') || (aaj.Status == 'Aborted')){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error Processing Job. Please Contact Administrator'));
            }
        }
        return batchJob;
    }

 Ay help would be highly appreciated.

 

Thanks in Advance

Best Answer chosen by rohitr
sfdcfoxsfdcfox

As written, you cannot achieve 100% coverage on this class, because you can't query a batch class "in progress" in a test method; it's a limitation of the framework.

Regardless, the basic method will be to do this:

1) Construct your extension controller.
2) Call Test.startTest().
3) Call the logic that kicks off the batch process.
4) Call getBatchJob(), this will test the "Queued" condition.
5) Call Test.stopTest(), this will move the job from Queued to Completed (or Failed). I assume your batch class is configurable via a constructor (if not, you should fix that first). This will let you test both the successful and failed messages.
6) Use assertions as necessary to make sure the jobs are working correctly.

 

Note, if you collapse Queued with Processing, and change your if-else assignments to ternary operators, you can achieve 100% coverage.