You need to sign in to do that
Don't have an account?

some help with testing
Hi
Newbie here! I have the following but can't cover most of the code, any help really appreciated.
public class MyProfilePageController {
private User user;
public boolean error = false;
private boolean isEdit = false;
public User getUser() {
return user;
}
public MyProfilePageController() {
user = [SELECT id, email, firstname, salutation__c
FROM User
WHERE id = :UserInfo.getUserId()];
if (user.usertype == 'GUEST') {
throw new NoAccessException();
}
}
public Boolean getIsEdit() {
return isEdit;
}
public void poedit() {
isEdit=true;
}
public pagereference save() {
if(user.salutation__c=='None'){
if(error!=TRUE){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The following are required fields:');
ApexPages.addMessage(msg);
}
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Salutation');
ApexPages.addMessage(msg);
error=TRUE;
}
if(user.firstname=='')
{
if(error!=TRUE){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The following are required fields:');
ApexPages.addMessage(msg);
}
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'First Name');
ApexPages.addMessage(msg);
error=TRUE;
}
}
if(error==TRUE){
error=false;
return null;
}
return null;
}
public void cancel() {
isEdit=false;
user = [SELECT id, email, username, companyName, salutation__c,billing_street__c,Billing_city__c,billing_county__c,billing_postcode__c,Billing_Address_same_as_Mailing__c,communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email, Practitioner_Speciality__c
FROM User
WHERE id = :UserInfo.getUserId()];
}
static testMethod void testSave() {
account acc = new account(name='Portal Registration Account');
insert acc;
contact con = new contact (firstname='First',lastname='Test', accountid=acc.id);
insert con;
Profile p = [select id from profile where name='OAWU Feb 11'];
User us = new user(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com',contactid=con.id, firstname='firstname');
insert us;
System.runAs(us) {
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(us.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.isEdit == false, 'isEdit should default to false');
controller.poedit();
System.assert(controller.isEdit == true);
controller.cancel();
System.assert(controller.isEdit == false);
controller.getUser().Fax = randFax;
controller.save();
System.assert(controller.Error == false);
controller.getisedit();
}
}
}
thanks
Once I woke up - I managed to fire the if statement by assigning salutation__c as 'None', however if I set firstname to '' it should run through the 2nd part of the next if statement but doesn't??
The problem seems to be that in the main class I am looking for a field that is blank, if I set the field to'' in the test statement it doesn't seem to recognise this as a blank field when running the test - if that makes sense can anyone shed any light?