+ Start a Discussion
Einstein TesterEinstein Tester 

Test class for batch class passed success but 0% code coverage

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);

 }
}
AnudeepAnudeep (Salesforce Developers) 
Can you add the following testMethod to your test class and see if it makes any difference?
 
static testmethod void test() {        
        Test.startTest();
        UserupdateBatch uub = new UserupdateBatch ();
        Id batchId = Database.executeBatch(uub);
        Test.stopTest();
    }

Just following the syntax as per the documentation

Anudeep
Einstein TesterEinstein Tester
Thank you for the reply Anudeep! I updated the above block of code in my test class. Here is the updated code below. Still, the other two methods passed but the new method that i added failed with this error in screenshot:Error

When i open the is in the errror, it is the id of the Automated user profile from our system. I don't why is this id getting errored out as i have not referened this profile anywhere in my main class.

Updated test class:
@isTest

public class UserUpdateBatchTest{

Public static TestMethod Void TestActiveUser() {

        Profile p = [Select Id From Profile Where Name='GP - Sales'];
        
        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='GP - Sales'];

        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);


 }
 static testmethod void test() {        
        Test.startTest();
        UserupdateBatch uub = new UserupdateBatch ();
        Id batchId = Database.executeBatch(uub);
        Test.stopTest();
    }
}