+ Start a Discussion

Any one help on below code write test class

global  class Batch_SubscriptionCMRRRollup implements Database.Batchable<SObject>{

global Database.QueryLocator start(Database.BatchableContext context) {
    //get all the billing accounts and the related subscriptions
    return Database.getQueryLocator([Select Id,Today_s_MRR__c,(Select Id,Zuora__MRR__c,Zuora__Status__c,Zuora__ServiceActivationDate__c from Zuora__Subscriptions__r) from Zuora__CustomerAccount__c]);

global void execute(Database.BatchableContext context, List<Zuora__CustomerAccount__c> listOfBillingAccounts) {

    //Iterate through all the billing accounts
    for(Zuora__CustomerAccount__c iteratingAccount: listOfBillingAccounts){
        //Iterate through all the subscriptions
        for(Zuora__Subscription__c iteratingSubscription : iteratingAccount.Zuora__Subscriptions__r){
            //add only those active subscriptions
            if((iteratingSubscription.Zuora__Status__c == 'Active' || 
                iteratingSubscription.Zuora__Status__c=='active') &&
                iteratingSubscription.Zuora__ServiceActivationDate__c <=  Date.Today() &&
                iteratingSubscription.Zuora__MRR__c != NULL){
                   iteratingAccount.Today_s_MRR__c += iteratingSubscription.Zuora__MRR__c;
    update listOfBillingAccounts;

global void finish(Database.BatchableContext context) {}}
To write the test classes for the batch class :
  1. ​First create the test records for Zuora__Subscriptions__r object, as per the conditions in your test class.
  2. Execute the database method in your test class
    • Batch_SubscriptionCMRRRollup object = new Batch_SubscriptionCMRRRollup();
    • Database.executeBatch(object);
If my suggestion(s) worked, do let me know by marking the answer as "Best Answer" right under the comment.
This will help the rest of the community should they have a similar issue in the future. 

Thank you..
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class,
public class Batch_SubscriptionCMRRRollupTest
    static testMethod void testMethod1()
        Account acct = new Account(Name = 'First last');
        insert acct;
        Zuora__CustomerAccount__c bAcc=new Zuora__CustomerAccount__c();
        bAcc.Name='Test Bill';
       // bAcc.Zuora__BillCycleDay__c = '1st of the month';
       // bAcc.Zuora__BillToAddress1__c = '125 fake street';
       // bAcc.Zuora__BillToAddress2__c = '';
       // bAcc.Zuora__BillToState__c = 'UT';
       // bAcc.Zuora__BillToPostalCode__c = '84058';
       // bAcc.Zuora__BillToCity__c = 'Provo';
       // bAcc.Zuora__BillToCountry__c = 'US';
       // bAcc.Send_to_Collections__c = false;
       // bAcc.Cancel_Billing_Account__c = false;
        Insert bAcc;
        Zuora__Subscription__c sub = new Zuora__Subscription__c();
        // add all required field Account lookup also
        sub.Zuora__Status__c  ='Active';
        sub.ServiceActivationDate__c   =Date.Today() -1;
        sub.Zuora__MRR__c    ='add';
        insert sub;

            Batch_SubscriptionCMRRRollup obj = new Batch_SubscriptionCMRRRollup();
let us know if this will help you

Hi Sekar,

Below is the test class logic that you can make use of

private class HelloWorldTestClass
static testMethod void validateHelloWorld() {
     Zuora__CustomerAccount__c ZuoraRec = new Zuora__CustomerAccount__c();
     ZuoraRec.Today_s_MRR__c = 'test';
     insert ZuoraRec;

    Zuora__Subscriptions__c Sub = new Zuora__Subscriptions__c();
sub.Zuora__Status__c = 'Active'
sub.Zuora__CustomerAccount__c = ZuoraRec.id;
insert sub;
Batch_SubscriptionCMRRRollup ClassInstance =  new Batch_SubscriptionCMRRRollup ();

I have inserted two records with that ever values that i see in your code. But there may be some other mandatory fields that you may be having it in those object. So please add those fields when inserting the record.

If this helps mark is as answer.

Manoj S

how to use system.assert equals
this is rollup of batch apex tht y any uses of systemassert equals()

 Invalid field ServiceActivationDate__c for SObject Zuora__Subscription__c at line 26 column 9
Gireesh ArniGireesh Arni
Shekar can you try this test class

private class Batch_SubscriptionCMRRRollup_Test{
   static Testmethod void Batch_SubscriptionCMRRRollup_Testmethod(){
       Zuora__CustomerAccount__c customer = new Zuora__CustomerAccount__c();
       fill some mandatory fields
       Insert customer;
       Zuora__Subscriptions__c subscription = new Zuora__Subscriptions__c();
       subscription.Zuora__CustomerAccountId = customer.Id;
       fill some fields here
       insert subscription;
       Zuora__CustomerAccount__c customertest =[Select id,name,Today_s_MRR__c where id =:customer.id];
       Batch_SubscriptionCMRRRollup objbatch = new Batch_SubscriptionCMRRRollup();
       ID batchprocessid = Database.excuteBatch(objbatch);
buggs sfdcbuggs sfdc