• Einstein Tester
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi All,

Can anyone help me here as to why i am getting code coverage as 0% even though my test class is running successfully?

The scenario is that i am fetching all the Frozen users from the UserLogin object and then updating a custom checkbox field on user named "Frozen" to 'True'' or 'False'. I am doing this to ensure the users who do not have access to view the standard Freeze button on user object Salesforce can indentify whether a user is frozen or not by seeing the custom Frozen field.

Below is the apex class which is working fine but the test class is showing 0% code covreage. 

Apex Class:
global class UserUpdateBatch implements Database.Batchable<sObject> {

    Public String query;

    global Database.QueryLocator start(Database.BatchableContext BC) {
       //if(!Test.isRunningTest()){
        query = 'SELECT Id, Frozen__c FROM User'+(Test.isRunningTest()?' LIMIT 200':'');
    //}  
        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.Frozen__c = True;
            }
            else{
                usr.Frozen__c = False;
                }
               
            }
       

        Database.SaveResult [] updateResult = Database.update(scope, false);
        
        for (Database.SaveResult r : updateResult)
        
        {
            if (!r.isSuccess()) 
            {
                for (Database.Error e : r.getErrors())
            { system.debug('Error'+e); }
            
            }
        
        }
               update scope;}
        global void finish(Database.BatchableContext BC) {
    }
 }

Test Class:
@isTest

public class UserUpdateBatchTest{

Public static TestMethod Void TestActiveUser() {

        Profile p = [Select Id From Profile Where Name='System Administrator'];

        User TestUser = New User( Alias = 'testuser', Email='Moo987@testFrozenUser.com',
         LastName= 'Testme', Firstname= 'you', Languagelocalekey= 'en_US', 
        LocaleSidKey='en_US', Frozen__c = True, TimeZoneSidkey='America/Los_Angeles', ProfileId=p.id,
         UserName= 'Moo987@testFrozenUser.com', EmailEncodingKey= 'UTF-8');
        insert TestUser;

    test.StartTest();

  ID batchjobid = Database.executeBatch(new UserupdateBatch());
    System.abortJob(batchjobid);

    test.StopTest(); 

        User assertUser= [select Id, Username, Frozen__c From User Where id=:TestUser.id][0];
     
        System.assertEquals(True, assertUser.Frozen__c);
    
    }

Public Static TestMethod Void TestInActiveUser() {

        Profile p = [Select Id From Profile Where Name='System Administrator'];

        User TestUser = New User( Alias = 'Newuser', Email='Moo987@testFrozenUser.com', 
        LastName= 'Testme', Firstname= 'you', Languagelocalekey= 'en_US', 
        EmailEncodingKey= 'UTF-8', Frozen__c = False, LocaleSidKey='en_US', 
        Profileid= P.id, TimeZoneSidkey='America/Los_Angeles',
        UserName= 'Moo987@testFrozenUser.com');
        List<User> lstUsr = new List <User>();                  
        Database.SaveResult userInsertResult= Database.insert(TestUser);
        lstUsr.add(TestUser);
        UserUpdateBatch sh1 = new UserUpdateBatch();
        
        //String sch = '0 0 23 * * ?'; system.schedule('Test', sch, sh1); 
        test.StartTest();
        testUser.IsActive =False;
        Update testuser;
        
   ID batchjobid = Database.executeBatch(new UserupdateBatch());
    System.abortJob(batchJobId);
    
    test.StopTest();

        User assertUser = [Select Id, Username,Frozen__c From User Where id=:TestUser.id][0];

        System.assertEquals(False, assertUser.Frozen__c);

 }
}
Hi All,

Can anyone help me here as to why i am getting code coverage as 0% even though my test class is running successfully?

The scenario is that i am fetching all the Frozen users from the UserLogin object and then updating a custom checkbox field on user named "Frozen" to 'True'' or 'False'. I am doing this to ensure the users who do not have access to view the standard Freeze button on user object Salesforce can indentify whether a user is frozen or not by seeing the custom Frozen field.

Below is the apex class which is working fine but the test class is showing 0% code covreage. 

Apex Class:
global class UserUpdateBatch implements Database.Batchable<sObject> {

    Public String query;

    global Database.QueryLocator start(Database.BatchableContext BC) {
       //if(!Test.isRunningTest()){
        query = 'SELECT Id, Frozen__c FROM User'+(Test.isRunningTest()?' LIMIT 200':'');
    //}  
        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.Frozen__c = True;
            }
            else{
                usr.Frozen__c = False;
                }
               
            }
       

        Database.SaveResult [] updateResult = Database.update(scope, false);
        
        for (Database.SaveResult r : updateResult)
        
        {
            if (!r.isSuccess()) 
            {
                for (Database.Error e : r.getErrors())
            { system.debug('Error'+e); }
            
            }
        
        }
               update scope;}
        global void finish(Database.BatchableContext BC) {
    }
 }

Test Class:
@isTest

public class UserUpdateBatchTest{

Public static TestMethod Void TestActiveUser() {

        Profile p = [Select Id From Profile Where Name='System Administrator'];

        User TestUser = New User( Alias = 'testuser', Email='Moo987@testFrozenUser.com',
         LastName= 'Testme', Firstname= 'you', Languagelocalekey= 'en_US', 
        LocaleSidKey='en_US', Frozen__c = True, TimeZoneSidkey='America/Los_Angeles', ProfileId=p.id,
         UserName= 'Moo987@testFrozenUser.com', EmailEncodingKey= 'UTF-8');
        insert TestUser;

    test.StartTest();

  ID batchjobid = Database.executeBatch(new UserupdateBatch());
    System.abortJob(batchjobid);

    test.StopTest(); 

        User assertUser= [select Id, Username, Frozen__c From User Where id=:TestUser.id][0];
     
        System.assertEquals(True, assertUser.Frozen__c);
    
    }

Public Static TestMethod Void TestInActiveUser() {

        Profile p = [Select Id From Profile Where Name='System Administrator'];

        User TestUser = New User( Alias = 'Newuser', Email='Moo987@testFrozenUser.com', 
        LastName= 'Testme', Firstname= 'you', Languagelocalekey= 'en_US', 
        EmailEncodingKey= 'UTF-8', Frozen__c = False, LocaleSidKey='en_US', 
        Profileid= P.id, TimeZoneSidkey='America/Los_Angeles',
        UserName= 'Moo987@testFrozenUser.com');
        List<User> lstUsr = new List <User>();                  
        Database.SaveResult userInsertResult= Database.insert(TestUser);
        lstUsr.add(TestUser);
        UserUpdateBatch sh1 = new UserUpdateBatch();
        
        //String sch = '0 0 23 * * ?'; system.schedule('Test', sch, sh1); 
        test.StartTest();
        testUser.IsActive =False;
        Update testuser;
        
   ID batchjobid = Database.executeBatch(new UserupdateBatch());
    System.abortJob(batchJobId);
    
    test.StopTest();

        User assertUser = [Select Id, Username,Frozen__c From User Where id=:TestUser.id][0];

        System.assertEquals(False, assertUser.Frozen__c);

 }
}
Hello all!

I'm trying to create a trigger that updates a custom "IsFrozen" field when a user's record is frozen.

The User object field, IsFrozen__c is supposed to see if the UserLogin object's IsFrozen field is updated to a value of true, and if so, update the user's record to true. I think this will greatly help us with the userdeactivation process.

My code is as follows:
trigger UserIsFrozen on User (before update) {
    
    for(User U: Trigger.new) {
        User oldU = Trigger.oldMap.get(U.Id);
        
        
        if(oldU.IsActive = true) {
            List<UserLogin> ULog = [SELECT Id, IsFrozen from UserLogin where UserId = :U.Id];
            List<UserLogin> newids = new List<UserLogin>();
            
            for(Userlogin USLog : ULog) {
                if(USLog.IsFrozen = true) {
                    U.IsFrozen__c = true;
                    newids.add(USLog);
                }
            }    
            if(newids.isEmpty()== false) {
                update newids;
                update U;
            }
        }
    }
}

The trigger isn't working right now. What am I doing wrong?