function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anuj Joshi 42Anuj Joshi 42 

Reset password test class with assert statement

Hi All,

Can anyone help me in wrting the test class with assert statement for the following code?
 
global with sharing class ResetPortalPassword{
    webService static String ResetPortalPassword(String userId)
    {
        System.ResetPasswordResult result = System.ResetPassword(userId,true);
        return String.valueOf(result);
    }
}



Thanks,
Anuj​
Best Answer chosen by Anuj Joshi 42
Manish  ChoudhariManish Choudhari
Hi Anuj,

Use below code to write test class for your webservice:
 
@isTest
public class TestRestPortalPassword {
	@isTest
    public static void ResetPortalPasswordTestMethod(){
        User u = new user();
        u.LastName = 'Test Code';
        u.Email = 'test@test.com';
        u.Alias = 'Tcode';
        u.Username = 'randomtest09876789@test.com';
        u.CommunityNickname = 'test12';
        u.LocaleSidKey = 'en_US';
        u.TimeZoneSidKey = 'GMT';
        u.ProfileID = '00e6F000002NyIh';
        u.LanguageLocaleKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        insert u;
        System.assert(u.Id != null);
        ResetPortalPassword rpp = new ResetPortalPassword();
        String result = ResetPortalPassword.ResetPortalPassword(String.valueOf(u.Id));
        System.assert(result != null);
    }
}
Just make sure you make below changes in above code:
> Make sure Username is unique.
> Make sure you replce the ProfileId with any 15 digit ProfileId in your org.

Let me know if you any further questions.

Thanks,
Manish

All Answers

Manj_SFDCManj_SFDC
Hey Anuj,

refer this https://developer.salesforce.com/forums/?id=906F0000000926DIAQ
Manish  ChoudhariManish Choudhari
Hi Anuj,

Use below code to write test class for your webservice:
 
@isTest
public class TestRestPortalPassword {
	@isTest
    public static void ResetPortalPasswordTestMethod(){
        User u = new user();
        u.LastName = 'Test Code';
        u.Email = 'test@test.com';
        u.Alias = 'Tcode';
        u.Username = 'randomtest09876789@test.com';
        u.CommunityNickname = 'test12';
        u.LocaleSidKey = 'en_US';
        u.TimeZoneSidKey = 'GMT';
        u.ProfileID = '00e6F000002NyIh';
        u.LanguageLocaleKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        insert u;
        System.assert(u.Id != null);
        ResetPortalPassword rpp = new ResetPortalPassword();
        String result = ResetPortalPassword.ResetPortalPassword(String.valueOf(u.Id));
        System.assert(result != null);
    }
}
Just make sure you make below changes in above code:
> Make sure Username is unique.
> Make sure you replce the ProfileId with any 15 digit ProfileId in your org.

Let me know if you any further questions.

Thanks,
Manish
This was selected as the best answer