You need to sign in to do that
Don't have an account?
divya 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);
}
}
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);
}
}
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%.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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,
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%.
If this solution helps, Please mark it as best answer.
Thanks,
Is the issue resolved now ?
Thanks,