+ Start a Discussion
virkvirk 

Test for the method

Can someone please help me in writing the test method for the following code?

global class UpdateStatusAcc implements Database.Batchable<sObject> {
 
 String query;
 LogAccountTasks__c logAccTasks;
 Boolean flag = false;
 List<Account> toUpdate;
 global UpdateStatusAcc() {
  
 }
 
 global Database.QueryLocator start(Database.BatchableContext BC) {
  String query = 'SELECT Id, AccountId__c, AccRecordTypeId__c, AccRTDeveloperName__c, Etat_actuel_de_la_relation_geo__c,Etat_futur_de_la_relation_geo__c, Flag__c, Type_de_reseau__c, CreationDate__c FROM LogAccountTasks__c WHERE flag__c=true ';
  return Database.getQueryLocator(query);
 }
    global void execute(Database.BatchableContext BC, List<LogAccountTasks__c> lstLogAccountTasks) {
  List<Id> accIds = new List<Id>();
  List<LogAccountTasks__c> lstTasks; //= new <List>logAccountTasks();
  List<Id> lstAccIds;
  //List<Account> TrigNew;
  Map<id, Account> TrigoldMap;
  List<Account> lstmemoryAcc;
  Account tmpAcc;
  if (lstLogAccountTasks == null || !lstLogAccountTasks.isEmpty()) {
          return;
  }
  
  //Add the AccountId to the Ids List
  for(LogAccountTasks__c LogAcc : lstLogAccountTasks) {
   //Prepare oldMap
   lstAccIds.add(LogAcc.AccountId__c);
   //Prepare List for simulate Trignew
   tmpAcc = null;
   tmpAcc.Id = LogAcc.AccountId__c;
   tmpAcc.Etat_relation_GEO__c = LogAcc.Etat_futur_de_la_relation_geo__c;
   tmpAcc.RecordTypeId = LogAcc.AccRecordTypeId__c;
   tmpAcc.R_seau_int_gr__c = logAcc.Type_de_reseau__c;
   lstmemoryAcc.add(tmpAcc);
  }
  //create Account object List
  //TrigNew = [SELECT Id, Name, RecordTypeID, R_seau_int_gr__c, Etat_relation_GEO__c FROM Account WHERE Id=:lstAccIds];
  //Create a virtual object of Account
//  for(lstLogAccountTasks)
        //TrigoldMap = new Map<Id, Account>([SELECT Id FROM Account], [SELECT Etat_relation_GEO__c FROM Account]);
        for (Account acc : [select Id, Etat_relation_GEO__c from Account  WHERE Id=:lstAccIds]) {
            TrigoldMap.put(acc.Id, acc);
        }
  UpdateStatus.hierarchyAccountDeploiment(lstmemoryAcc, TrigoldMap);
/*  if (logAccountTasks != null && logAccountTasks.size() > 0) {
   for (Account a : logAccountTasks)
        a.Etat_relation_GEO__c = 'Déploiement';
  }
*/
//     update logAccountTasks;
//     flag=true;
 }
 
 global void finish(Database.BatchableContext BC) {
  //if(flag == true && logAccountTasks != null && logAccountTasks.size() > 0)
  // delete logAccountTasks;
  //System.debug('hello');
 }
 
}

Test Code

@isTest
private class UpdateStatusAccTest {
 
 @isTest(seeAllData=true) static void test_updateStatusAcc() {
  
  List<LogAccountTasks__c> lstAccountTasks = new List<LogAccountTasks__c>();
   String aId = '00561000001DTdh';
   LogAccountTasks__c logAccountTask;
   //0123E00000006VZQAY
  // Implement test code
     Test.startTest();
   List<Account> lstTestAcc = [SELECT Id, Name, RecordTypeId, RecordType.DeveloperName, Etat_relation_geo__c, R_seau_int_gr__c FROM Account WHERE Etat_relation_geo__c='Terminé' LIMIT 4];
         // TEST SUR statut "Déploiement"
         for(Account myacc : lstTestAcc) {
             logAccountTask = new LogAccountTasks__c();
             logAccountTask.Name =myacc.Name;
             logAccountTask.AccRecordTypeId__c = aId;
             logAccountTask.AccRTDeveloperName__c = myacc.RecordType.DeveloperName;
             logAccountTask.AccountId__c = myacc.Id;
             logAccountTask.Etat_actuel_de_la_relation_geo__c = 'Terminé';
             logAccountTask.Etat_futur_de_la_relation_geo__c = 'Déploiement';
             logAccountTask.Flag__c = true;
             logAccountTask.CreationDate__c = Date.today();
             logAccountTask.Type_de_reseau__c = myacc.R_seau_int_gr__c;            
             lstAccountTasks.add(logAccountTask);
         }
         // TEST SUR statut "Terminé"
   lstTestAcc = [SELECT Id, Name, RecordTypeId, RecordType.DeveloperName, Etat_relation_geo__c, R_seau_int_gr__c FROM Account WHERE Etat_relation_geo__c='Déploiement' LIMIT 4];
         for(Account myacc : lstTestAcc) {
             logAccountTask = new LogAccountTasks__c();
             logAccountTask.Name =myacc.Name;
             logAccountTask.AccRecordTypeId__c = aId;
             logAccountTask.AccRTDeveloperName__c = myacc.RecordType.DeveloperName;
             logAccountTask.AccountId__c = myacc.Id;
             logAccountTask.Etat_actuel_de_la_relation_geo__c = 'Déploiement';
             logAccountTask.Etat_futur_de_la_relation_geo__c = 'Terminé';
             logAccountTask.Flag__c = true;
             logAccountTask.CreationDate__c = Date.today();
             logAccountTask.Type_de_reseau__c = myacc.R_seau_int_gr__c;            
             lstAccountTasks.add(logAccountTask);
         }
         insert lstAccountTasks;
        
         List<Account> lstAccount;
         for(LogAccountTasks__c logAccTask : lstAccountTasks){
          Account acc = new Account();
          acc.Id = logAccTask.AccountId__c;
          acc.Etat_relation_GEO__c = logAccTask.Etat_futur_de_la_relation_geo__c;
          acc.RecordTypeId = logAccTask.AccRecordTypeId__c;
    acc.R_seau_int_gr__c = logAccTask.Type_de_reseau__c;
    lstAccount.add(acc);
         }
     UpdateStatusAcc updateSatusAcc = new UpdateStatusAcc();
  ID idBatch = Database.executeBatch(updateSatusAcc);
      System.assert(!lstAccountTasks.isEmpty());
      System.assertNOTEquals(logAccountTask.AccountId__c,null);
         System.debug('test_updateStatusAcc ('+ idBatch +') : simulation tasks ');
     Test.stopTest();
     //List<Account> lstAccountLogResults = [SELECT Id FROM Account];
     //System.debug('list result' + lstAccountLogResults.size());
 
 }
 
}

I have already written some test code given as follows.

But I am not sure of the last snippet of Account objects. Whether it is right or not.

Thanks in advance.