function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AmandaSilberAmandaSilber 

Test Class not working for Batch Apex

I have tried and tried to write and rewrite this test class and still cannot get more than 33% coverage! Please help!!!

 

Class:

global class MVbatch implements Database.Batchable<sObject>, Schedulable
{
date myDate = date.today();

global String Query = 'Select ID, MASA_Contract_Termination_Date__c, MASA_Contract_Effective_Date__c, A1_Amount_Payable__c, A1_Validation_Frequency__c, A2_Amount_Payable__c, A2_Validation_Frequency__c,A3_Amount_Payable__c,A3_Validation_Frequency__c, A4_Amount_Payable__c,   A4_Validation_Frequency__c,A5_Amount_Payable__c,A5_Validation_Frequency__c,A6_Amount_Payable__c,A6_Validation_Frequency__c,Month_Year__c' + 'FROM Partnership_Account__c';


global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator(Query);}

global void execute(Database.BatchableContext BC,List<sObject> scope){
    List<Partnership_Account__c> lstpaccounts = new List<Partnership_Account__c>();
    
    for(sObject s : scope){Partnership_Account__c paccount = (Partnership_Account__c)s;
        if(paccount.MASA_Contract_Termination_Date__c>= myDate && paccount.MASA_Contract_Effective_Date__c <=myDate){
                           
Marketing_Validations__c mv = new Marketing_Validations__c
(Name = paccount.Month_Year__c,
Partnership_Account__c = paccount.id,
X1_Validation_Cycle__c = paccount.C1_Validation_Frequency__c,
X2_Validation_Cycle__c = paccount.C2_Validation_Frequency__c,
X3_Validation_Cycle__c = paccount.C3_Validation_Frequency__c,
X4_Validation_Cycle__c = paccount.C4_Validation_Frequency__c,
X5_Validation_Cycle__c = paccount.C5_Validation_Frequency__c,
X6_Validation_Cycle__c = paccount.C6_Validation_Frequency__c,
X7_Validation_Cycle__c = paccount.C7_Validation_Frequency__c,
X8_Validation_Cycle__c = paccount.C8_Validation_Frequency__c,
X9_Validation_Cycle__c = paccount.C9_Validation_Frequency__c,
X10_Validation_Cycle__c =paccount.C10_Validation_Frequency__c,
X11_Validation_Cycle__c = paccount.A1_Validation_Frequency__c,
X12_Validation_Cycle__c = paccount.A2_Validation_Frequency__c,
X13_Validation_Cycle__c = paccount.A3_Validation_Frequency__c,
X14_Validation_Cycle__c = paccount.A4_Validation_Frequency__c,
X15_Validation_Cycle__c = paccount.A5_Validation_Frequency__c,
X16_Validation_Cycle__c = paccount.A6_Validation_Frequency__c,
X17_Validation_Cycle__c = paccount.A7_Validation_Frequency__c,
X18_Validation_Cycle__c = paccount.A8_Validation_Frequency__c,
X1_Amount_Payable__c= paccount.C1_Amount_Payable__c,
X2_Amount_Payable__c= paccount.C2_Amount_Payable__c,
X3_Amount_Payable__c= paccount.C3_Amount_Payable__c,
X4_Amount_Payable__c= paccount.C4_Amount_Payable__c ,
X5_Amount_Payable__c= paccount.C5_Amount_Payable__c ,
X6_Amount_Payable__c= paccount.C6_Amount_Payable__c ,
X7_Amount_Payable__c= paccount.C7_Amount_Payable__c ,
X8_Amount_Payable__c= paccount.C8_Amount_Payable__c ,
X9_Amount_Payable__c= paccount.C9_Amount_Payable__c ,
X10_Amount_Payable__c= paccount.C10_Amount_Payable__c , 
X11_Amount_Payable__c= paccount.A1_Amount_Payable__c ,
X12_Amount_Payable__c= paccount.A2_Amount_Payable__c , 
X13_Amount_Payable__c= paccount.A3_Amount_Payable__c , 
X14_Amount_Payable__c= paccount.A4_Amount_Payable__c , 
X15_Amount_Payable__c= paccount.A5_Amount_Payable__c , 
X16_Amount_Payable__c= paccount.A6_Amount_Payable__c , 
X17_Amount_Payable__c= paccount.A7_Amount_Payable__c , 
X18_Amount_Payable__c= paccount.A8_Amount_Payable__c , 
HLC_Responsible_for_MASA__c= paccount.HLC_Responsible_for_MASA__c,
HLM__c= paccount.HLM__c,
DM__c= paccount.DM__c,
BDM__c= paccount.BDM_Name__c);


insert mv;
system.debug('************'  + mv.id);
}
}
}


global void finish(Database.BatchableContext BC)
{
}

global void execute(SchedulableContext sc)
{
}
}

 Test Class:

@isTest
private class testmvbatch{

  
    static testmethod void test() {
  
        String query = 'Select ID, MASA_Contract_Termination_Date__c, MASA_Contract_Effective_Date__c, A1_Amount_Payable__c, Month_Year__c'  + 
        'FROM Partnership_Account__c' + 'WHERE MASA_Contract_Termination_Date__c <= today()';
        
        Partnership_Account__c[] palist = new List<Partnership_Account__c>();
        for (integer i=0; i<10;i++)  {
            Partnership_Account__c pa = new Partnership_Account__c (Name = 'testaccount' + i, MASA_Contract_Termination_Date__c = date.today(), MASA_Contract_Effective_Date__c = date.today(), A1_Amount_Payable__c = 123.45);
            palist.add(pa);
            }
            insert palist;    
    
        test.startTest();
        mvbatch mb=new mvbatch();
        database.executebatch(mb,200);
        test.stopTest();
        

     
     }
}

 

 

Any help would be REALLY appreciated!! Thanks!!

AmandaSilberAmandaSilber

Anyone? =(

uaryamarkituaryamarkit

I am facing same probelm. I am not able to receive more than 13%. Is something broken after winder 13' release? 

kcpluspluskcplusplus

I'm seeing the same thing, as well as other strange test class issues. I've noticed that brackets and whitespace lines are actually counting in test class compilation. 

 

--KC

AmandaSilberAmandaSilber

Yeah this particular class had 88% before the release. Since then it dropped to 33% and I have tried everything to get it up with no luck. At least I know it's not just me.

AmandaSilberAmandaSilber

So, I pasted over my code with what I had in Production and now get 91% coverage in Sandbox (but still 41% in production??).   And I figured out that the coverage difference between the two is based on the  WHERE statement.  If I change the field that it is pointing to, or add another field - it completely throws it off and changes the coverage to 41%.  I'm so confused!!!!