You need to sign in to do that
Don't have an account?
Bryan Telford
Apex test class issue with value set in VF page
I have the following Apex class and test clas.
public class passwordReset
{
Decimal minimumPasswordLength;
Decimal minimumPINNumberLength;
public String selectedOption { get; set; }
String requestId = ApexPages.currentPage().getParameters().get('request_id');
Encompass_Password_Service__c settings = Encompass_Password_Service__c.getOrgDefaults();
String currentUserContactdId = [select User.Contact.Id from User where id = :UserInfo.getUserId()].Contact.Id;
//String currentUserContactdId = '0030R00000MNIJS';
Decimal pinExpiration = settings.PIN_Number_Expiration_Minutes__c;
Decimal maxPasswordResets = settings.Maximum_Resets_Per_Day__c;
Decimal maxAccountUnlocks = settings.Maximum_Unlocks_Per_Day__c;
//Integer additionalMinutes = pinExpiration.intValue();
public void requestPin()
{
String requestGuid = Random.generateGuid();
//Generate PIN number
integer numberOfResets= [Select count() from Encompass_Password_Reset__c where CreatedDate = TODAY and User_Selected_Option__c = 'Reset'];
integer numberOfUnlocks= [Select count() from Encompass_Password_Reset__c where CreatedDate = TODAY and User_Selected_Option__c = 'Unlock'];
System.debug('selected option: ' + this.selectedOption);
if(this.selectedOption == 'Reset' && numberOfUnlocks < 5){
//insert event
Encompass_Password_Reset__c resetEvent = new Encompass_Password_Reset__c(PIN_Number__c = '1234',
Requested_Date_Time__c = System.Now(),
Employee__c = currentUserContactdId,
Request_Guid__c = requestGuid,
Username__c = 'test.lo',
User_Selected_Option__c = 'Reset',
PIN_Number_Expiration__c = System.Now().addMinutes(60));
insert resetEvent;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'A PIN was sent to your email.The PIN will expire in 60 minutes.'));
}
else if(this.selectedOption == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select an option.'));
}
}
}
TEST CLASS
@isTest
public class passwordResetTest {
private static testmethod void requestPinForPasswordReset() {
Test.setCurrentPage(new PageReference('request_pin'));
Encompass_Password_Reset__c reset = createRequest(requestType); insert reset;
encompassPasswordService controller = new encompassPasswordService();
Test.startTest();
controller.requestPin();
Test.stopTest();
List<ApexPages.Message> msgList = ApexPages.getMessages();
for(ApexPages.Message msg : ApexPages.getMessages()) {
System.assertEquals('A PIN was sent to your email.The PIN will expire in 60 minutes.', msg.getSummary());
System.assertEquals(ApexPages.Severity.CONFIRM, msg.getSeverity());
}
}
private static Encompass_Password_Reset__c createRequest(String requestType) {
Account acc = new Account(Name = 'Sample Account'); insert acc;
Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;
TestHelper.ObjectMother mo = new TestHelper.ObjectMother();
User usr = mo.createUser(con.id, con.Email = 'test345@test.com', acc.Name); insert usr;
//Integer additionalMinutes = settings.PIN_Number_Expiration_Minutes__c.intValue();
String requestGuid = Random.generateGuid();
String selectedOption = requestType;
Encompass_Password_Reset__c event = new Encompass_Password_Reset__c(
Request_Guid__c = requestGuid,
PIN_Number__c = '477405',
User_Selected_Option__c = requestType,
Requested_Date_Time__c = System.Now(),
Username__c = 'lo.test',
Employee__c = usr.Contact.Id,
PIN_Number_Expiration__c = System.Now().addMinutes(60));
System.debug('request type: ' + event.User_Selected_Option__c);
return event;
}
}
The test fails with this message.
System.AssertException: Assertion Failed: Expected: A PIN was sent to your email.The PIN will expire in 60 minutes., Actual: Please select an option.
When I look at the debug log, the "selected option" is null, therefore the test returns the message as specified by this condition in the controller:
else if(this.selectedOption == null)
How can I modify the test class to recognize the selected option that is set in the VF page?
public class passwordReset
{
Decimal minimumPasswordLength;
Decimal minimumPINNumberLength;
public String selectedOption { get; set; }
String requestId = ApexPages.currentPage().getParameters().get('request_id');
Encompass_Password_Service__c settings = Encompass_Password_Service__c.getOrgDefaults();
String currentUserContactdId = [select User.Contact.Id from User where id = :UserInfo.getUserId()].Contact.Id;
//String currentUserContactdId = '0030R00000MNIJS';
Decimal pinExpiration = settings.PIN_Number_Expiration_Minutes__c;
Decimal maxPasswordResets = settings.Maximum_Resets_Per_Day__c;
Decimal maxAccountUnlocks = settings.Maximum_Unlocks_Per_Day__c;
//Integer additionalMinutes = pinExpiration.intValue();
public void requestPin()
{
String requestGuid = Random.generateGuid();
//Generate PIN number
integer numberOfResets= [Select count() from Encompass_Password_Reset__c where CreatedDate = TODAY and User_Selected_Option__c = 'Reset'];
integer numberOfUnlocks= [Select count() from Encompass_Password_Reset__c where CreatedDate = TODAY and User_Selected_Option__c = 'Unlock'];
System.debug('selected option: ' + this.selectedOption);
if(this.selectedOption == 'Reset' && numberOfUnlocks < 5){
//insert event
Encompass_Password_Reset__c resetEvent = new Encompass_Password_Reset__c(PIN_Number__c = '1234',
Requested_Date_Time__c = System.Now(),
Employee__c = currentUserContactdId,
Request_Guid__c = requestGuid,
Username__c = 'test.lo',
User_Selected_Option__c = 'Reset',
PIN_Number_Expiration__c = System.Now().addMinutes(60));
insert resetEvent;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'A PIN was sent to your email.The PIN will expire in 60 minutes.'));
}
else if(this.selectedOption == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select an option.'));
}
}
}
TEST CLASS
@isTest
public class passwordResetTest {
private static testmethod void requestPinForPasswordReset() {
Test.setCurrentPage(new PageReference('request_pin'));
Encompass_Password_Reset__c reset = createRequest(requestType); insert reset;
encompassPasswordService controller = new encompassPasswordService();
Test.startTest();
controller.requestPin();
Test.stopTest();
List<ApexPages.Message> msgList = ApexPages.getMessages();
for(ApexPages.Message msg : ApexPages.getMessages()) {
System.assertEquals('A PIN was sent to your email.The PIN will expire in 60 minutes.', msg.getSummary());
System.assertEquals(ApexPages.Severity.CONFIRM, msg.getSeverity());
}
}
private static Encompass_Password_Reset__c createRequest(String requestType) {
Account acc = new Account(Name = 'Sample Account'); insert acc;
Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;
TestHelper.ObjectMother mo = new TestHelper.ObjectMother();
User usr = mo.createUser(con.id, con.Email = 'test345@test.com', acc.Name); insert usr;
//Integer additionalMinutes = settings.PIN_Number_Expiration_Minutes__c.intValue();
String requestGuid = Random.generateGuid();
String selectedOption = requestType;
Encompass_Password_Reset__c event = new Encompass_Password_Reset__c(
Request_Guid__c = requestGuid,
PIN_Number__c = '477405',
User_Selected_Option__c = requestType,
Requested_Date_Time__c = System.Now(),
Username__c = 'lo.test',
Employee__c = usr.Contact.Id,
PIN_Number_Expiration__c = System.Now().addMinutes(60));
System.debug('request type: ' + event.User_Selected_Option__c);
return event;
}
}
The test fails with this message.
System.AssertException: Assertion Failed: Expected: A PIN was sent to your email.The PIN will expire in 60 minutes., Actual: Please select an option.
When I look at the debug log, the "selected option" is null, therefore the test returns the message as specified by this condition in the controller:
else if(this.selectedOption == null)
How can I modify the test class to recognize the selected option that is set in the VF page?
Test.startTest();
controller.selectedOption = 'Reset';
controller.requestPin();
Test.stopTest();
See bob_buzzard's post for reference.
https://developer.salesforce.com/forums/?id=906F00000008xclIAA