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!
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.
Comments