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
divya gourigari 14divya gourigari 14 

Getting 73% in test class not crossing

Apex class:
public class Deactiveusercls {
@future    
public static void processAsync() {
    
     DateTime dt = DateTime.now();
        String dateString = dt.format().replace('/',' ');
       System.debug(dateString);
      String[] strDTDivided = dateString.split(' ');
        system.debug('strDTDivided'+strDTDivided);
       string month = strDTDivided.get(0);
       system.debug('month'+month);
      string day = strDTDivided.get(1).replace(',', '');
       system.debug('day'+day);
      string year = strDTDivided.get(2);
      system.debug('year'+year);

      string hour = strDTDivided.get(3).split(':').get(0);
      string minute = strDTDivided.get(3).split(':').get(1);
      string second = '00';
      String ampm = strDTDivided.get(4);
    /*  if (strDTDivided.get(4) == 'PM')
     {
     hour = String.ValueOf(Integer.ValueOf(hour) + 12);
     }*/
   // string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
   string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ' ' +ampm; 
    system.debug('stringDate'+stringDate);
    
    map<string, Deactiveuser__c> ObjMap = new map<string, Deactiveuser__c>();
    LIst<Deactiveuser__c> activeLst =[select id,UserName__c,Name__c,TerminateDate__c from Deactiveuser__c where TerminateDate__c =:stringDate];
    system.debug('activeLst'+activeLst);
    for(Deactiveuser__c obj: activeLst)
    {
        if (obj.UserName__c != Null)
        {
            ObjMap.put(obj.UserName__c, obj);
        }
    }
    System.debug('ObjMap'+ObjMap);
    List<User> users = [SELECT isActive,email,Username,Alias FROM User WHERE Alias IN :ObjMap.KeySet()  and  isActive = true];
    system.debug('users'+users);
    List<User> userUpdateList = new List<User>();
   // String dateFormat = 'yyyy-MM-dd\'T\'HH:mm:ss\'Z\''.replace('\'T\'',' ').replace('\'Z\'','');
    for(User u: users)
    {
        u.isActive = false;
        userUpdateList.add(u);
   }
    if(userUpdateList.size() > 0)
    {
        update userUpdateList;
        system.debug('userUpdateList'+userUpdateList);
    }  
}   
}

Test Class:

@isTest
public class DeactiveuserclsTest {
 static testMethod void deactive() {
   
     
     
      Profile pf= [Select Id from profile where Name='System Administrator']; 
     User u = new User(
     ProfileId = pf.Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'test',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
  
     );
     insert u;
     
     Deactiveuser__c d=new Deactiveuser__c();
     d.Name__c ='testUser';
     d.UserName__c = 'test';
     d.TerminateDate__c = '2022-4-25 6 PM';
     insert d;
    
     u.IsActive = false;
    // update u;
    
    Test.startTest();
     Deactiveusercls.processAsync();
      Test.stopTest(); 
     
//   User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
        System.assertEquals(false, u.IsActive);  
     }
}
Best Answer chosen by divya gourigari 14
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Divya,

There is no issus in your test class except the Terminate Date field. It should also be formated same as in apex class.

As the field is not as expected it is not giving any resuts in the query and remaining part is not getting covered. Now this is covering 100%.
@isTest
public class DeactiveuserclsTest {
 static testMethod void deactive() {
   
     
     
      Profile pf= [Select Id from profile where Name='System Administrator']; 
     User u = new User(
     ProfileId = pf.Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'test',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
  
     );
     insert u;
     DateTime dt = DateTime.now();
        String dateString = dt.format().replace('/',' ');
       System.debug(dateString);
      String[] strDTDivided = dateString.split(' ');
        system.debug('strDTDivided'+strDTDivided);
       string month = strDTDivided.get(0);
       system.debug('month'+month);
      string day = strDTDivided.get(1).replace(',', '');
       system.debug('day'+day);
      string year = strDTDivided.get(2);
      system.debug('year'+year);

      string hour = strDTDivided.get(3).split(':').get(0);
      string minute = strDTDivided.get(3).split(':').get(1);
      string second = '00';
      String ampm = strDTDivided.get(4);
   string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ' ' +ampm;
     Deactiveuser__c d=new Deactiveuser__c();
     d.Name__c ='testUser';
     d.UserName__c = 'test';
     d.TerminateDate__c = stringDate;
     insert d;
    
     u.IsActive = false;
    // update u;
    
    Test.startTest();
     Deactiveusercls.processAsync();
      Test.stopTest(); 
     
//   User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
        System.assertEquals(false, u.IsActive);  
     }
}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Divya,

Can you share what part of the class is covered and what is not covered so based on it experts can suggest you how to improve the coverage for that part.

Thanks,
 
divya gourigari 14divya gourigari 14
  that red mark part is not covering  
User-added image
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Divya,

There is no issus in your test class except the Terminate Date field. It should also be formated same as in apex class.

As the field is not as expected it is not giving any resuts in the query and remaining part is not getting covered. Now this is covering 100%.
@isTest
public class DeactiveuserclsTest {
 static testMethod void deactive() {
   
     
     
      Profile pf= [Select Id from profile where Name='System Administrator']; 
     User u = new User(
     ProfileId = pf.Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'test',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
  
     );
     insert u;
     DateTime dt = DateTime.now();
        String dateString = dt.format().replace('/',' ');
       System.debug(dateString);
      String[] strDTDivided = dateString.split(' ');
        system.debug('strDTDivided'+strDTDivided);
       string month = strDTDivided.get(0);
       system.debug('month'+month);
      string day = strDTDivided.get(1).replace(',', '');
       system.debug('day'+day);
      string year = strDTDivided.get(2);
      system.debug('year'+year);

      string hour = strDTDivided.get(3).split(':').get(0);
      string minute = strDTDivided.get(3).split(':').get(1);
      string second = '00';
      String ampm = strDTDivided.get(4);
   string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ' ' +ampm;
     Deactiveuser__c d=new Deactiveuser__c();
     d.Name__c ='testUser';
     d.UserName__c = 'test';
     d.TerminateDate__c = stringDate;
     insert d;
    
     u.IsActive = false;
    // update u;
    
    Test.startTest();
     Deactiveusercls.processAsync();
      Test.stopTest(); 
     
//   User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
        System.assertEquals(false, u.IsActive);  
     }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Divya,

Is the issue resolved now ?

Thanks,