Testing methods mocking ApexAsynJob

Hristo Penchev

I have the following issue. Here is my target apex class that Ineed to test:

class TargetClass {
    public static Id BatchId {get; set;} 
    public void methodOne() {
        //adding an ApexAsyncJob in database and assign its ID ot BatchId
    }

    @Remote
    public static String methodTwo(string batchId) {
        //gets the ApexAsynJob inserted in previous method from database and         //do some operations with it 
    }
}

And here is my testing class

@isTest
public TargetClassTest() {
    static testmethod void test() {
        test.startTest();
        TargetClass tgtClass = new TargetClass();
        tgtClass.methodOne();
        TargetClass.methodTwo(TargetClass.BatchId);
    }
}

When methodOne is done and methdTwo is called, we have the BatchId property assigned but the AsynApexJob is not in the database yet so methodtwo throws an exception. How can I solve it and finish my tests. I have the following ideas 1. run methodTwo whenever the AsynApexJob is in the database. How can I do this? An endless loop checking the database doesn't work as we hit the limit of queries. 2. Mocking an AsyncApexJob. I dont necessarily need the AsynApexJob inserted in methodOne. Any would work. I don't know how to do it. When I tried to insert one in the database, I got an error message that INSERT insn't available in AsynApexJob. It would be fine if someone can help me. Thanks!

EricSSH

You can querying out the jobs, so after you schedule the job you can do this and then assert that the job was actually scheduled ..

List<AsyncApexJob> jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob];
System.assertEquals(jobInfo.size(), 1); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular Unit Testing - Mocking methods/closures in same service

From Dev

AngularJS testing: Mocking $httpBackend - Handling Success and Error Methods

From Dev

Struggling with unit testing and mocking

From Dev

Mocking a Singleton for Unit Testing

From Dev

Mocking CKContainer for Unit Testing

From Dev

Mocking CKContainer for Unit Testing

From Java

Mocking static methods with Mockito

From Dev

mocking controller methods in rspec

From Dev

Partial Mocking/Faking in Unit Testing

From Dev

Unit testing with Jasmine, mocking a constructor

From Dev

Mocking a ViewModel for unit testing with Moq?

From Dev

Testing/Mocking Workers in Controller Specs

From Dev

Refactoring and mocking to support unit testing

From Dev

Mocking/Testing HTTP Get Request

From Dev

Mocking public method of testing object

From Dev

Mocking static methods with PowerMock and Mockito

From Dev

Mocking class to test its methods

From Dev

Mocking class methods with OCMock not working

From Dev

Mocking a call with chained methods and arguments

From Dev

Mocking patched class methods is not working

From Dev

PHPUnit 9 - Mocking void methods

From Dev

Mocking class methods with OCMock not working

From Dev

RSpec mocking all methods on object

From Dev

Mocking methods in Puppet rspec tests

From Dev

Mocking methods from a parent Class

From Dev

partial mocking of methods of concrete class

From Dev

Mocking properties to test validation methods

From Dev

Angular Testing - Trouble mocking Service with $http dependency

From Dev

node js unit testing: mocking require dependency