+ Start a Discussion
Haseeb Ahmad 9Haseeb Ahmad 9 

Apex class not getting code coverage

Hello all,

I have the apex class which not getting any code coverage would you able to help me fix it or what is wrong here, thank you. 

Class: 
 
public class ChangePassword 
{
    @InvocableMethod(
        label = 'User Password Reset'
        description = 'User Password Reset'
    )
    
    public static List <String> getUserId (List <ID> ids){
        List<String> userId = new List <String> ();
        List<User> userList = [SELECT Id from User WHERE 
                                        IsActive = true AND 
                                        CreatedDate = TODAY AND 
                                        LastLoginDate = NULL AND
                                        LastPasswordChangeDate = NULL AND
                                        Id in :ids 
                                        LIMIT 100];   
        for(User u : userList)
                {
                      system.resetPassword(u.Id, true);
                      System.debug('DONE: ' + u.Id);
                }
        return userId;
    }
}

test:
 
@isTest
private class ChangePasswordTest {

    @testSetup
    static void ChangePassword () {

        Profile p = [ SELECT id FROM Profile WHERE name = 'Standard User' ];

        Test.startTest();   
        User u = new User ();
        u.firstName = 'test1';
        u.lastName = 'test2';
        u.ProfileId = p.Id ;
        u.Username = 'test@email4x.com' ;
        u.Email = 'test@email.com';
        u.Alias = 't1';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.LocaleSidKey = 'en_US' ;
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.LanguageLocaleKey = 'en_US' ;
        Insert u ;
        system.resetPassword(u.Id, true);
        System.debug('DONE: ' + u.Id);
        Test.stopTest();

    }

}

​​​​​​​

 
Best Answer chosen by Haseeb Ahmad 9
CharuDuttCharuDutt
Hii Haseeb Ahmed
Try Below Test Class Made Some Change
@isTest
private class ChangePasswordTest {

    @istest
    Public static void ChangePassword () {

        Profile p = [ SELECT id FROM Profile WHERE name = 'Standard User' ];

        Test.startTest();   
        User u = new User ();
        u.firstName = 'test1';
        u.lastName = 'test2';
        u.ProfileId = p.Id ;
        u.Username = 'test@email4x.com' ;
        u.Email = 'test@email.com';
        u.Alias = 't1';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.LocaleSidKey = 'en_US' ;
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.LanguageLocaleKey = 'en_US' ;
        Insert u ;
        list<id> lstId = new list<id>();
        lstId.Add(u.Id);
        //system.resetPassword(u.Id, true);
        //System.debug('DONE: ' + u.Id);
        ChangePassword.getUserId(lstId);
        Test.stopTest();

    }

}
Please Mark It As Best Answer If It Helps Thank You!
 

All Answers

CharuDuttCharuDutt
Hii Haseeb Ahmed
Try Below Test Class Made Some Change
@isTest
private class ChangePasswordTest {

    @istest
    Public static void ChangePassword () {

        Profile p = [ SELECT id FROM Profile WHERE name = 'Standard User' ];

        Test.startTest();   
        User u = new User ();
        u.firstName = 'test1';
        u.lastName = 'test2';
        u.ProfileId = p.Id ;
        u.Username = 'test@email4x.com' ;
        u.Email = 'test@email.com';
        u.Alias = 't1';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.LocaleSidKey = 'en_US' ;
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.LanguageLocaleKey = 'en_US' ;
        Insert u ;
        list<id> lstId = new list<id>();
        lstId.Add(u.Id);
        //system.resetPassword(u.Id, true);
        //System.debug('DONE: ' + u.Id);
        ChangePassword.getUserId(lstId);
        Test.stopTest();

    }

}
Please Mark It As Best Answer If It Helps Thank You!
 
This was selected as the best answer
Haseeb Ahmad 9Haseeb Ahmad 9
Hi CharuDutt,

This worked thank you so much for your help.