You need to sign in to do that
Don't have an account?
buggs sfdc
test class on UserLogin Batch Class
HI,
Need help covering test class for below Apex Batch class,i really appreciate if someone respond me quickly.
Thanks In Advance!
Need help covering test class for below Apex Batch class,i really appreciate if someone respond me quickly.
Thanks In Advance!
global class UpdateBatch_Usr_login implements Database.Batchable<sObject> { String query; global Database.QueryLocator start(Database.BatchableContext BC) { query = 'SELECT Id, isActive, Status__c FROM User'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<User> scope) { List<Id> usrLogLst = new List<Id>(); for(UserLogin ul : [SELECT UserId, isFrozen from UserLogin where isFrozen = true and UserId IN: scope]){ usrLogLst.add(ul.userid); } for(User usr : scope){ if(usrLogLst.contains(usr.id)){ usr.status__c = 'Frozen'; } else{ if(usr.isActive==true){ usr.status__c = 'Active'; } else{ usr.status__c = 'Inactive'; } } } update scope; } global void finish(Database.BatchableContext BC) { } }
Thanks for responding me quickly.
Here iam getting below errors,and on line 20 we are getting an error (IsFrozen is not valid),the reason we have IsFrozen field in userlogin.
Some how i resolved the issue,but now iam seeing different error, i tried creating a list and checked with if(lst.size()>0) condition.
But still iam seeing the same error can u please check
Stack Trace Class.TestUpdateBatch_Usr_login.testActiveUser: line 56, column 1" that you encounter is because the following query at line # 54, 55 does not return a valid User record and hence the value for assertUser in the testActiveUser() is null. Check if the Where clause is returning the expected User record.
Test Class like below
With Above changes you will get 88% (16/18).
You can not create userLogin record in your test class. and isFrozen is available on UserLogin object not on user object
can you please look into it,iam getting the below Error.
I hope you also added below line in your batch job
Please check and let me know where iam lagging,i really appreciate your time for helping me on resolving it.
For my Batch apex
global class Communityuserinactivation implements Database.stateful,Database.Batchable<sObject>
{
public List<User> userrecordsprocessed= new List<User>();
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='Select id,LastLoginDate,username,CommunityNickname,isActive,FederationIdentifier from user where isActive=True AND Profile.name=\'System Administrator\' LIMIT 10';
System.debug('+++query'+query);
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<user> scope)
{
System.debug('++++scope'+scope);
// userrecordsprocessed=scope;
List<User> lisuser= new List<User>();
for(user us:scope)
{
String usrname=us.username;
String CmmunityNickname=us.CommunityNickname;
Datetime dt=us.LastLoginDate;
datetime dt2= System.now();
integer intDays =dt.Date().daysBetween(dt2.Date());
if(intDays>=10)
{
if(usrname.length()>34)
{
usrname=usrname.substring(0,usrname.length()-7);
}
if(CmmunityNickname.length()>34)
{
CmmunityNickname=CmmunityNickname.substring(0,usrname.length()-7);
}
us.Username=usrname+'.old'+String.valueOf(Math.random()).substring(2, 4);
us.CommunityNickname=CmmunityNickname+'.old'+String.valueOf(Math.random()).substring(2, 4);
us.FederationIdentifier='';
//us.CCO_ID__c='';
us.IsActive=False;
userrecordsprocessed.add(us);
System.debug('++++lisuser'+lisuser);
}
}
update userrecordsprocessed;
}
global void finish(Database.BatchableContext BC)
{
string csvHeader = 'User ID, UserName,Lastlogindate,CommunityNickname,FederationIdentifier,IsActive \n';
string mainContent = csvHeader;
for(User us:userrecordsprocessed)
{
string recordString = us.id+','+us.Username+','+us.LastLoginDate+','+us.CommunityNickname+','+us.FederationIdentifier+','+us.IsActive+'\n';
mainContent+=recordString;
}
Messaging.EmailFileAttachment csvAttcmnt = new Messaging.EmailFileAttachment ();
blob csvBlob = Blob.valueOf(mainContent);
string csvname= 'Inactiveusers.csv';
csvAttcmnt.setFileName (csvname);
csvAttcmnt.setBody (csvBlob);
Messaging.SingleEmailMessage singEmail = new Messaging.SingleEmailMessage ();
String [] toAddresses = new list<string> {'vsattaru@cisco.com'};
singEmail.setToAddresses (toAddresses);
String subject ='Inactiveuser list';
singEmail.setSubject (subject);
singEmail.setPlainTextBody ('users not logged in last 6 months');
singEmail.setFileAttachments (new Messaging.EmailFileAttachment []{csvAttcmnt});
Messaging.SendEmailResult [] r = Messaging.sendEmail (new Messaging.SingleEmailMessage [] {singEmail});
}
}
I am writing the below Test Class but unable to get Code Coverage, it was stuck at 72% and not getting executed. Could you please help!!!
Here is my test class
@isTest
private class testclassbatchapex
{
public static testmethod void testquery()
{
Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
User testUser = new User(Alias = 'newUser', Email='newusser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles',
ProfileId = p.Id, UserName = 'newusser@testorg.com');
Database.SaveResult userInsertResult = Database.insert(testUser);
test.startTest();
User Usr = [Select Id, Name, isActive from User Where Id = :testUser.Id];
Usr.isActive = true;
update Usr;
database.executeBatch(new Communityuserinactivation());
test.stopTest();
User assertUser = [Select Id, UserName From User Where UserName = :'varshas@accenture.com'];
//system.assertEquals('true', 'false');
//System.assertEquals('true', assertUser.IsActive);
}
testmethod static void Communityuserinactivationnn()
{
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
User testUser = new User(
Alias = 'newUser', Email='fastusserr@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName = 'fastusserr@testorg.com');
Database.SaveResult userInsertResult = Database.insert(testUser);
test.startTest();
database.executeBatch(new Communityuserinactivation());
test.stopTest();
User assertUser = [Select Id, UserName From User Where UserName = :'fastusserr@testorg.com'][0];
//System.assertEquals('Active', assertUser.Status__c);
}
testmethod static void testme()
{
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
User testUser = new User(
Alias = 'newUser', Email='fastuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName = 'fastuser@testorg.com',
IsActive = false);
Database.SaveResult userInsertResult = Database.insert(testUser);
test.startTest();
database.executeBatch(new Communityuserinactivation());
test.stopTest();
User assertUser = [Select Id, UserName From User Where UserName = :'fastuser@testorg.com'][0];
//System.assertEquals('False', assertUser.isActive);
}
}
I have the same requirement and i want to show you my approach, which will work also.
My solution works until three licences are active on runnning org.
I emulate UserLogin into a custom inner class and use this for test purposes. Feel free to criticize and comment this solution. I would be very happy about that.
Below my scheduled batchjob:
Below my testclass: