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
KR_ForceKR_Force 

test class help for Batch class

Can someone please help to buld the test class for below batch class?

global class Batch1234 implements Database.Batchable<sobject>, Database.Stateful
{
    public String query;
    global String error;
    global database.queryLocator start(Database.BatchableContext BC)
    {       String query = 'select AccountId,Account.Product_Subscriptions__c, Id,oracle_product_family__c, Product_Family__c FROM Asset where oracle_product_family__c!= \'\'';
            return database.getQueryLocator(query); 
    } 
   
    global void execute(Database.BatchableContext BC, List<sObject> scope)
    { 
        List<asset> assetList =(List<Asset>)scope;
        set<id>acIDs=new set<Id>();
        Map<Id,Set<String>> actPsubs = new Map<Id,Set<String>>();
        List<Account> updateAccountList = new List<Account>();
        Map<Id,Account> updateAccountMap = new Map<Id,Account>();
        System.Debug('assetList ****'+assetList);
            for(Asset a :assetList){
                String prodstring='';
                set<string>unique=new set<String>(); 
                for(String s:a.oracle_product_family__c.split(':')){
                unique.add(s); 
                }
                if(a.Account.Product_Subscriptions__c!=null && a.Account.Product_Subscriptions__c!=''){
                    System.Debug('at#1 ****');
                    for(String Key:a.Account.Product_Subscriptions__c.split(';')){
                            unique.add(key);
                            System.Debug('at#2 ****');
                        } 
                    }
                if(unique.size()!=0){
                        for(String st : unique ){ 
                            prodstring = prodstring +';'+ st;
                        }
                    }
                System.Debug('prodstring****'+prodstring);
                updateAccountMap.put(a.AccountId,new Account(Id = a.AccountId, Product_Subscriptions__c = prodstring));
                }
                System.Debug('updateAccountMap ****'+updateAccountMap);
                if(updateAccountMap.size() != 0){
                Database.SaveResult[] srList = Database.update(updateAccountMap.values(), false);
                        for(Database.SaveResult sr : srList)
                        {
                            for(Database.Error e : sr.getErrors())
                            error += '\n ' + e.getMessage();
                        }
                    }
         
       }
   
    global void finish(Database.BatchableContext BC){
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { finish.Email(BC)});
   }  
}

===============================================================================================
test class
======================================================================

static testmethod void testBatchClass() {
           /*List <Asset> ast= new List<Asset>();
           Set<String> uniq=new Set<String>();
           Account a = new Account();
           a.Name = 'test account';
           insert a;
           for(integer i = 0; i<200; i++){
           Asset a1 = new Asset(Name='testAsset'+i, RVBD_Product_Family__c= 'test'+i+';',Instance_Number__c='77777777XX'+i,AccountID=a.id);
           ast.add(a1);
           uniq.add(a1.RVBD_Product_Family__c);
           }
           insert ast;
           String prodstring='';
           for(String st : uniq){ 
            prodstring = prodstring +';'+ st;
            }
            a.Product_Subscriptions__c=prodstring;
           
            Test.startTest();
            Batch1123 bc = new Batch123);
            bc.query = 'select AccountId, Id, Product_Family__c FROM Asset where AccountID=a.id limit 200';
            bc.error='TestError';
            ID batchprocessid = Database.executeBatch(bc);
            Test.stopTest();
Best Answer chosen by KR_Force
AshlekhAshlekh
Hi

You don't need to proivde query in batct as you have already put in start method. And Start method will execute when you execute the batch.

Just provde relvent data to your records which your creating in test class which met the criteria of your query which is define in start method and also meet the condation which you have applied in execute method.


static testmethod void testBatchClass() {
           
List <Asset> ast= new List<Asset>();

           Set<String> uniq=new Set<String>();

           Account a = new Account();

           a.Name = 'test account';

           a.Product_Subscriptions__c='test;test:';

           insert a;

           for(integer i = 0; i<200; i++){

 Asset a1 = new Asset(Name='testAsset'+i, RVBD_Product_Family__c= 'test'+i+';',Instance_Number__c='77777777XX'+i,AccountID=a.id);

a1.oracle_product_family__c='test:tes';

           ast.add(a1);

           uniq.add(a1.RVBD_Product_Family__c);

           }
      
     insert ast;

           String prodstring='';

           for(String st : uniq){ 

            prodstring = prodstring +';'+ st;

            }
            a.Product_Subscriptions__c=prodstring;
           
            Test.startTest();
            Batch1123 bc = new Batch123);
            bc.error='TestError';
            ID batchprocessid = Database.executeBatch(bc);

            Test.stopTest();
}
IF it helps you than please mark it as a solution and ENJOY APEX

All Answers

Ankit AroraAnkit Arora
Are you facing any problem with the code, or you want us to write the exact test class for you?
AshlekhAshlekh
Hi

You don't need to proivde query in batct as you have already put in start method. And Start method will execute when you execute the batch.

Just provde relvent data to your records which your creating in test class which met the criteria of your query which is define in start method and also meet the condation which you have applied in execute method.


static testmethod void testBatchClass() {
           
List <Asset> ast= new List<Asset>();

           Set<String> uniq=new Set<String>();

           Account a = new Account();

           a.Name = 'test account';

           a.Product_Subscriptions__c='test;test:';

           insert a;

           for(integer i = 0; i<200; i++){

 Asset a1 = new Asset(Name='testAsset'+i, RVBD_Product_Family__c= 'test'+i+';',Instance_Number__c='77777777XX'+i,AccountID=a.id);

a1.oracle_product_family__c='test:tes';

           ast.add(a1);

           uniq.add(a1.RVBD_Product_Family__c);

           }
      
     insert ast;

           String prodstring='';

           for(String st : uniq){ 

            prodstring = prodstring +';'+ st;

            }
            a.Product_Subscriptions__c=prodstring;
           
            Test.startTest();
            Batch1123 bc = new Batch123);
            bc.error='TestError';
            ID batchprocessid = Database.executeBatch(bc);

            Test.stopTest();
}
IF it helps you than please mark it as a solution and ENJOY APEX

This was selected as the best answer
KR_ForceKR_Force
Thnks Ashlekh Gera (D-Horse), it worked.