You need to sign in to do that
Don't have an account?
rajubalaji
how to write a test class for batch apex with flag value as false?
HI Everyone,
I was writing test class for batch apex i was written all ways but still i was in 18% code coveagre if anyone help me to reach 100% coverage.
Batch Apex:
public class UserRegistrationStatusUpdate implements Database.Batchable<sObject> {
public UserRegistrationStatusUpdate() {}
public Database.QueryLocator start(Database.BatchableContext BC) {
System.debug('UserRegistrationStatusUpdate start');
if (Label.User_RegistrationCheck_TimeInHours == '-1') {
return Database.getQueryLocator([Select Id, AccountID__c, Email__c, External_Parent_Id__c from Hub_Parent__c Where Product_Status__c = 'Active' and Registration_Completed_Date__c != null]);
}
else {
Datetime registrationStartDay = Datetime.now().addHours(-Integer.valueOf(Label.user_RegistrationCheck_TimeInHours));
return Database.getQueryLocator([Select Id, AccountID__c, Email__c, External_Parent_Id__c from Hub_Parent__c Where Product_Status__c = 'Active' and Registration_Completed_Date__c != null and Registration_Completed_Date__c >= :registrationStartDay]);
}
}
public void execute(Database.BatchableContext BC, List<Hub_Parent__c> ParentList) {
try {
System.debug('userRegistrationStatusUpdate execute count ' + ParentList.size());
List<user__c> userToUpdateList = new List<user__c>();
List<user__c> userList;
String emailHashed;
String memberIDHashed;
Blob b;
for (Hub_Parent__c Parent : ParentList) {
b = Crypto.generateDigest('SHA-256', Blob.valueOf(Parent.Email__c.toLowerCase()));
emailHashed = EncodingUtil.base64Encode(b);
userList = [Select Id, Registered_User__c from user__c Where AccountID__c = :Parent.AccountID__c and Eligibility_User__c = false and EmailHashed__c = :emailHashed Limit 1];
if (userList.size() > 0) {
if (userList[0].Registered_User__c == false) {
userList[0].Registered_User__c = true;
userToUpdateList.add(userList[0]);
}
}
else {
if (Parent.External_Parent_Id__c != null) {
b = Crypto.generateDigest('SHA-256', Blob.valueOf(Parent.External_Parent_Id__c.toLowerCase()));
memberIDHashed = EncodingUtil.base64Encode(b);
userList = [Select Id, Registered_User__c from user__c Where AccountID__c = :Parent.AccountID__c and Eligibility_User__c = false and Registered_User__c = false and MemberIDHashed__c = :memberIDHashed Limit 1];
if (userList.size() > 0) {
userList[0].Registered_User__c = true;
userToUpdateList.add(userList[0]);
}
}
}
}
if (userToUpdateList.size() > 0) {
update userToUpdateList;
}
} catch (Exception ex) {
System.debug('userRegistrationStatusUpdate Exception :' + ex.getMessage());
}
}
public void finish(Database.BatchableContext BC) {}
}
Test Class for above Batch Apex:
@isTest
public class UserRegistrationStatusUpdateTest {
static testMethod void testUserRegistrationStatusUpdate(){
Test.startTest();
TestController.createCustomSettings();
TestController.createEnviornmentSettings();
Hub_Parent__c pt = TestController.getMockParentObject();
List<User__c> User = new List<User__c>();
User__c U= new User__c();
U.FirstName__c = 'nMM';
U.LastName__c = 'MMM';
U.Gender__c = 'Male';
U.Email__c = pt.Email__c;
Blob b = Crypto.generateDigest('SHA-256', Blob.valueOf(U.Email__c.toLowerCase()));
U.EmailHashed__c = EncodingUtil.base64Encode(b);
U.DOB__c = system.today();
U.Member_ID__c = '10755633045998625107';
Blob b1 = Crypto.generateDigest('SHA-256', Blob.valueOf(U.Member_ID__c.toLowerCase()));
U.MemberIDHashed__c = EncodingUtil.base64Encode(b1);
U.AccountID__c = pt.AccountID__c;
insert U;
UserRegistrationStatusUpdate obj = new UserRegistrationStatusUpdate();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
Thanks InAdvance.
I was writing test class for batch apex i was written all ways but still i was in 18% code coveagre if anyone help me to reach 100% coverage.
Batch Apex:
public class UserRegistrationStatusUpdate implements Database.Batchable<sObject> {
public UserRegistrationStatusUpdate() {}
public Database.QueryLocator start(Database.BatchableContext BC) {
System.debug('UserRegistrationStatusUpdate start');
if (Label.User_RegistrationCheck_TimeInHours == '-1') {
return Database.getQueryLocator([Select Id, AccountID__c, Email__c, External_Parent_Id__c from Hub_Parent__c Where Product_Status__c = 'Active' and Registration_Completed_Date__c != null]);
}
else {
Datetime registrationStartDay = Datetime.now().addHours(-Integer.valueOf(Label.user_RegistrationCheck_TimeInHours));
return Database.getQueryLocator([Select Id, AccountID__c, Email__c, External_Parent_Id__c from Hub_Parent__c Where Product_Status__c = 'Active' and Registration_Completed_Date__c != null and Registration_Completed_Date__c >= :registrationStartDay]);
}
}
public void execute(Database.BatchableContext BC, List<Hub_Parent__c> ParentList) {
try {
System.debug('userRegistrationStatusUpdate execute count ' + ParentList.size());
List<user__c> userToUpdateList = new List<user__c>();
List<user__c> userList;
String emailHashed;
String memberIDHashed;
Blob b;
for (Hub_Parent__c Parent : ParentList) {
b = Crypto.generateDigest('SHA-256', Blob.valueOf(Parent.Email__c.toLowerCase()));
emailHashed = EncodingUtil.base64Encode(b);
userList = [Select Id, Registered_User__c from user__c Where AccountID__c = :Parent.AccountID__c and Eligibility_User__c = false and EmailHashed__c = :emailHashed Limit 1];
if (userList.size() > 0) {
if (userList[0].Registered_User__c == false) {
userList[0].Registered_User__c = true;
userToUpdateList.add(userList[0]);
}
}
else {
if (Parent.External_Parent_Id__c != null) {
b = Crypto.generateDigest('SHA-256', Blob.valueOf(Parent.External_Parent_Id__c.toLowerCase()));
memberIDHashed = EncodingUtil.base64Encode(b);
userList = [Select Id, Registered_User__c from user__c Where AccountID__c = :Parent.AccountID__c and Eligibility_User__c = false and Registered_User__c = false and MemberIDHashed__c = :memberIDHashed Limit 1];
if (userList.size() > 0) {
userList[0].Registered_User__c = true;
userToUpdateList.add(userList[0]);
}
}
}
}
if (userToUpdateList.size() > 0) {
update userToUpdateList;
}
} catch (Exception ex) {
System.debug('userRegistrationStatusUpdate Exception :' + ex.getMessage());
}
}
public void finish(Database.BatchableContext BC) {}
}
Test Class for above Batch Apex:
@isTest
public class UserRegistrationStatusUpdateTest {
static testMethod void testUserRegistrationStatusUpdate(){
Test.startTest();
TestController.createCustomSettings();
TestController.createEnviornmentSettings();
Hub_Parent__c pt = TestController.getMockParentObject();
List<User__c> User = new List<User__c>();
User__c U= new User__c();
U.FirstName__c = 'nMM';
U.LastName__c = 'MMM';
U.Gender__c = 'Male';
U.Email__c = pt.Email__c;
Blob b = Crypto.generateDigest('SHA-256', Blob.valueOf(U.Email__c.toLowerCase()));
U.EmailHashed__c = EncodingUtil.base64Encode(b);
U.DOB__c = system.today();
U.Member_ID__c = '10755633045998625107';
Blob b1 = Crypto.generateDigest('SHA-256', Blob.valueOf(U.Member_ID__c.toLowerCase()));
U.MemberIDHashed__c = EncodingUtil.base64Encode(b1);
U.AccountID__c = pt.AccountID__c;
insert U;
UserRegistrationStatusUpdate obj = new UserRegistrationStatusUpdate();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
Thanks InAdvance.
Greetings!
I would suggest you to check which lines are covered and then you can work on the code which has not covered.
Reference:https://apexcoder.com/2016/11/08/how-to-write-test-class-for-batch-apex-in-salesforce/
Please mark it as best answer if it helps you to fix the issue.
Thank you!
Regards,
Shirisha Pathuri