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
viswadaviswada 

Test coverage On User Class

Hi all ,

      

 I  have a class   On User object  , I  have Written Test class for that that class   , I am Gettin  only 51% ,  How can i   improve that above 75%.  Plaease help  me , My  

 

Apex calss:

 

public class IdeaForgotPasswordController extends IdeaController {
public String email {get; set;}

public IdeaForgotPasswordController() {
}

public PageReference forgotPassword() {
if (!isValid()) {
return null;
}

String username = null;
if (email != null && email != '') {
List<User> users = [select username from user where username = :email];
if (users.size() == 1) {
// exact match on username
username = users.get(0).username;
}
else {
users = [select username from user where email = :email];
if (users.size() == 1) {
// exact match on email address
username = users.get(0).username;
}
}
}

if (username == null) {
addErrorMessage('User not found');
return null;
}

boolean success = Site.forgotPassword(username);
if (success) {
PageReference pr = new PageReference(URL_FORGOT_PASSWORD_CONFIRM);
pr.setRedirect(true);
return pr;
}
return null;
}

public boolean isValid() {
boolean valid = true;

if (email == '') {
addErrorMessage('Email address is required.');
valid = false;
}

return valid;
}

private void addErrorMessage(String message) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, message);
ApexPages.addMessage(msg);
}

}

 

 

Test  class Fort that Class

------------------------------------

 

@isTest
private clAss IdeaForgotPasswordControllerTest
{
static TestMethod Void TestIdeaForgetpassword()
{

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'eerer',
EmailEncodingKey='UTF-8', LastName='bbbbb', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id, Email='adityavyas.sf@gmail.com',
TimeZoneSidKey='America/Los_Angeles', UserName='adityavyas.sf@gmail.com');
List<User> users = [select username from user where username = :U.email];

System.runAs(u) {
// The following code runs as user 'u'

System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId()); }

IdeaForgotPasswordController ideaforget= New IdeaForgotPasswordController();
Pagereference page=ideaforget.forgotPassword();

}
}

vishal@forcevishal@force

Try the below code, you haven't given any email and so it won't go any futher in the code.

 

@isTest
private clAss IdeaForgotPasswordControllerTest
{
static TestMethod Void TestIdeaForgetpassword()
{

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'eerer',
EmailEncodingKey='UTF-8', LastName='bbbbb', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id, Email='adityavyas.sf@gmail.com',
TimeZoneSidKey='America/Los_Angeles', UserName='adityavyas.sf@gmail.com');
List<User> users = [select username from user where username = :U.email];

System.runAs(u) {
// The following code runs as user 'u'

System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId()); }

IdeaForgotPasswordController ideaforget= New IdeaForgotPasswordController();

ideaforget.email = 'test@test.com';
Pagereference page=ideaforget.forgotPassword();

}
}

viswadaviswada

Hi Thanks For replaying ,  its, working  , but code coverage is 61% only.

 

    boolean success = Site.forgotPassword(username);
        if (success) {
            PageReference pr = new PageReference(URL_FORGOT_PASSWORD_CONFIRM);
            pr.setRedirect(true);
            return pr;
        }
        return null;
    }

 

 

this code  is not covering, please help me