0

Apex Class:

global with sharing class SchedularForBatchApex implements Schedulable {
    global void execute(SchedulableContext sc) {
        String CLASS_NAME1 = 'Ru_Actual__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME1);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Ru_Actual__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME2 = 'Revenue_Actual__c';
        try {

               BatchTargtUpdate b1 = new BatchTargtUpdate(CLASS_NAME2);
        Database.executeBatch(b1);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Revenue_Actual__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME3 = 'Direct_Revenue_Actual__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME3);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Direct_Revenue_Actual__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME4 = 'Actual_Cost__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME4);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Actual_Cost__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME5 = 'Average_Deal__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME5);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Average_Deal__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME6 = 'Forecast_Actual__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME6);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Forecast_Actual__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME7 = 'Number_of_Won_Opportunities__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME7);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Number_of_Won_Opportunities__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME8 = 'Number_of_No_Decision_Opportunities__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME8);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Number_of_No_Decision_Opportunities__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME9 = 'Ru_Actual__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME9);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Ru_Actual__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME10 = 'Number_of_Closed_Opportunities__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME10);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Number_of_Closed_Opportunities__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
        String CLASS_NAME11 = 'Account_Revenue__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME11);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Account_Revenue__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
         String CLASS_NAME12 = 'Fulfillment_Form__c';
        try {

                BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME12);
                Database.executeBatch(b);

        }catch(Exception ex){
            system.debug('Exception in BatchTargtUpdate(Fulfillment_Form__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME12, 'Execute',
                                   'Exception Line Number:' + ex.getLineNumber() +
                                   '~ Exception Type-' + ex.getTypeName()+                                                             
                                   '~ Exception Message-' + ex.getmessage());
        } 

        try {

               SalesPersonBatchTargtUpdate b16 = new SalesPersonBatchTargtUpdate('Revenue_Actual__c','Actual_Revenue_for_oppty');
                Database.executeBatch(b16);

        }catch(Exception ex){
            system.debug('Exception in SalesPersonBatchTargtUpdate(Fulfillment_Form__c) Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 

        try {

              SFBatchTargetUpdate b17 = new SFBatchTargetUpdate();
              Database.executeBatch(b17);
        }catch(Exception ex){
            system.debug('Exception in SFBatchTargetUpdate() Execute Process submision and error is--->'+ex);

            BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
        } 
        }


 }

Test Class:

@isTest(SeeAllData='true')
private class SchedularForBatchApexTest {
    public static Datetime dt = Datetime.now().addMinutes(1);
    public static String CRON_EXP = '0 '+ dt.minute() + ' * ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
    public static testMethod void validateSchedularForBatchApex() {
        System.SchedulableContext SC;
        Database.QueryLocator QL;
        Database.BatchableContext BC;
        List<Target__c> tgtList=new List<Target__c>();
        Database.DMLOptions dml = new Database.DMLOptions();
        dml.DuplicateRuleHeader.AllowSave = true; 
        //Record For BatchTargtUpdate 
        Target__c t1=new Target__c(
            RecordTypeId =Schema.SObjectType.Target__c.getRecordTypeInfosByName().get('BCC').getRecordTypeId(),
            Financial_Year__c='FY2018',
            Vertical__c='Marketing',
            Forecast_Type__c='Monthly',
            Forecast_Period__c='B1-(Apr)',
            Bill_Target__c=1500000,
            Bill_Actual__c=1500000,
            Revenue_Target__c=1500000,
            Revenue_Actual__c=1500000,
            Direct_Revenue_Target__c=1500000,
            Direct_Revenue_Actual__c=1500000,
            Margin_Target__c=0.5,
            Strategic_Account__c=1500000,
            Fulfillment_Target__c=12,
            Contribution__c=1500000,
            Utilization_Target__c=0.5,
            Demand_Fulfillment__c=10,
            Forecast_Target__c=0.5,
            Forecast_Actual__c=0.5,
            Net_Win_Rate_Target__c=0.5,
            Average_Deal_Size_Actual__c=1500000,
            Rate_Target__c=0.5,
            Actual_Cost__c=1500000
        );
        tgtList.add(t1);
        Database.SaveResult sr=Database.Insert(t1,dml);
        if (sr.isSuccess()) {   
            System.debug('Duplicate account has been inserted in Salesforce!'); 
        }
        BatchTargtUpdate obj=new GCPBatchTargtUpdate('Actual_Cost__c');
        SchedularForBatchApex obje=new SchedularForBatchApex();
        Test.startTest();
        Id jobId = System.schedule('Batch Apex Scheduler', CRON_EXP, new SchedularForBatchApex());
        System.assert(jobId != null);
        obje.execute(SC); 
        Test.stopTest();
    }
}

When I try to run Test Class then it throws me an error message:

No more than one executeBatch can be called from within a test method" ... System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch

The Reason I have enabled SeeAllData=true is because when Batch class run,it puts the data in Reports and I can not create Report instances in Test classes.

I am new in Scheduler/Batch Apex Class hence need suggestion of how to do code coverage for scheduler test class including best practices if any.

Can Someone please tell me what I am missing in Test Class?

  • I've not used batch apex enough (read: at all), so I don't think I'll be able to contribute a meaningful answer. However, isn't the error message pretty clear? You can only call executeBatch() once. Is there some reason why you need to run these batch jobs in parallel instead of chaining via the finish() method? (chaining would make testing easier, but you'd still need one testMethod per batch job). Having 17 batch classes that need to be executed seems like a red flag to me, would it make sense for you to do some consolidation? – Derek F Sep 21 '17 at 12:58
  • Well there is no specific reason but we have to run this batch job in every 2 hours including all methods.Some other developer has written it earlier.I just want to Cover the test class.but if u could suggest a better way then that would be great.. – Dhananjay Patil Sep 21 '17 at 13:09
3

Refactor your code to put your BatchClass arguments in an array (list or map), then the code iterates through the collection, starting each one in turn. Then your test method coverage issue becomes much simpler as going through the loop once (because testmethods won't start batch#2+) will cover all the code.

If you need to verify unique results from each of the 19 batch jobs, then you'll need 19 testmethods, one for each. SeeAllData=true is a recipe for issues later. You can use the fflib Unit of Work pattern to abstract the updates to Report allowing you to verify (see ApexMocks) that the objects were built

More sophisticated approaches would exploit ApexMocks (based on Mockito) wherein you can verify that some methodX got called 19 times without actually having it run

Alternatively, you can use the StubAPI and roll your own mocks. ApexMocks is built atop the StubAPI.

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.