0
@isTest(seeAllData = false)
private class interfacetest {

    public static testMethod void testInterfaceScheduler(){
        Product2 testproduct = new Product2(Name='test product',ProductCode = '112233');
        insert testproduct;
        Test.startTest();
        InterfaceCalloutMock fakeResponse = new InterfaceCalloutMock(200);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        InterfaceSchedule InterfaceSc = new InterfaceSchedule();
        String sch = '0 0 23 * * ?';
        system.schedule('Test Interface Scheduler', sch, InterfaceSc);
        Test.stopTest();
        List<Product_Image__c> images = [select Id from Product_Image__c where Product__c =:testproduct.Id];
        system.assertEquals(1, images.size());
    }

    public static testMethod void testInterfaceBatch(){
        Product2 testproduct = new Product2(Name='test product',ProductCode = '112233');
        insert testproduct;
        Test.startTest();
        InterfaceCalloutMock fakeResponse = new InterfaceCalloutMock(200);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        InterfaceBatch Batchtest = new InterfaceBatch();
        database.executebatch(Batchtest,100);
        Test.stopTest();
        List<Product_Image__c> images = [select Id from Product_Image__c where Product__c =:testproduct.Id];
        system.assertEquals(1, images.size());
    }

    public class InterfaceCalloutMock implements HttpCalloutMock {
        Integer responseCode {get;set;}
        InterfaceCalloutMock(Integer responseCode){
            this.responseCode = responseCode;
        }
        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse resp = new HttpResponse();
            resp.setStatusCode(responseCode);
            resp.setBody('[{"id":"31A3FCE7-DDEF-40D1-8365A8CAA8809348"}]');
            return resp;
        }
    }

} 

The schedulable class just executes the batch apex in the execute method.

For some reason testInterfaceScheduler FAILS assertion, while testInterfaceBatch PASS the assertion. I am unable to figure out why, any help is appreciated.

Thank you!

  • Updating the question with source code for the batch and scheduleable apex would be helpful in troubleshooting the problem. – arut Aug 19 at 13:46
1

The reason for this is that in testInterfaceScheduler you are trying to execute two asynchronous transactions after Test.stoptest()

  • The schedulable
  • The batchable

Test.stopTest() only executes the first async transaction before continuing to the assert statement. Your debug log will show the batchable being executed but it actually happens (in test context) after the assert.

Now, a way to fix this is to do proper unit testing

  • when you test a schedulable; you simply need to test its constructor and execute() method. What you really care about is that the execute() started a batchable. And you can simply verify that there is an AsyncApexJob for the batchable. You don't have to test that the batchable starts.
  • when you test a batchable, you provide mock objects for the start() to find, then verify the execute() and finish() do what you want

If you are passing constructor args to the batchable, then you can verify the batchable is called with the proper args by doing:

@IsTest static void testBatchable() {
   InterfaceSchedule schedulable = new InterfaceSchedulable(args);
   Test.startTest();
   schedulable.execute(null);  // SchedulableContext can't be constructed
   Test.stoptest();  // batchable will execute now
   // do asserts

}

where the Schedulable execute() looks like this

public void execute(SchedulableContext sc) {
  InterfaceBatch batchable = new InterfaceBatch(this.args);  // from Dependency-injected constructor args  
  Database.executeBatch(batchable);
}    

That is, you dependency inject into the schedulable some args you want to pass to the batchable. This means you'll have a no args constructor and a "with args" constructor for your schedulable

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.