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

Help with a test class
Hi I have an apex class:
public with sharing class ProfileTabUserController {
// Purpose: Custom Chatter profile page
private ApexPages.StandardController c;
// Getter methods you can call from your Visualforce page, e.g. {!viewingMyProfile }
public User subjectUser { get; set; }
public boolean viewingMyProfile { get; set; } // Whether or not I’m viewing my profile
public String viewerID { get; set; } // UID string for the viewing user
public String subjectID { get; set; } // UID string for the subject user (being viewed)
// Constructor method for the controller
public ProfileTabUserController(ApexPages.StandardController stdController) {
c = stdController;
subjectID = getTargetSFDCUID();
// If we're operating inside a tab running inside of a profile...
if (subjectID != null) {
// Inject the sfdc.userId URL parameter value into the id param
// so the std User controller loads the right User record
ApexPages.currentPage().getParameters().put('id', subjectID);
}
// Load the User record for the user whose profile we’re viewing
this.subjectUser = (User)stdController.getRecord();
Id viewer = Id.valueOf(UserInfo.getUserId());
Id subject = Id.valueOf(subjectID);
viewingMyProfile = (viewer == subject);
viewerID = UserInfo.getUserId();
}
// Fetches URL parameter passed into a profile tab indicating which user is being viewed
private String getTargetSFDCUID() {
return
ApexPages.currentPage().getParameters().get('sfdc.userId');
}
// Overrides StandardController save method to force reload of current page afterwards
public PageReference save() {
c.save();
return ApexPages.currentPage();
}
// Overrides StandardController cancel method to force page reload
public PageReference cancel() {
c.cancel();
return ApexPages.currentPage();
}
}
But I need help with a test class. I have this but I keep getting error and it won't let me save. Can anyone help me fix the test class or help me to create a new one? Thanks in advance, here is the test class.
@isTest
private class ProfileTabControllerTests {
private static testMethod void testProfileTabController() {
User u = [select Id from User where Id = :UserInfo.getUserId() ];
PageReference pageRef = Page.Chatter_Custom;
Test.setCurrentPage(pageRef);
ApexPages.StandardController con = new ApexPages.StandardController(u);
ProfileTabUserController ext = new ProfileTabUserController(con);
System.assertEquals(u.Id, ext.subjectID);
System.assert(viewingMyProfile);
String cancelPage = ext.cancel().getUrl();
String savePage = ext.save().getUrl();
}
}
public with sharing class ProfileTabUserController {
// Purpose: Custom Chatter profile page
private ApexPages.StandardController c;
// Getter methods you can call from your Visualforce page, e.g. {!viewingMyProfile }
public User subjectUser { get; set; }
public boolean viewingMyProfile { get; set; } // Whether or not I’m viewing my profile
public String viewerID { get; set; } // UID string for the viewing user
public String subjectID { get; set; } // UID string for the subject user (being viewed)
// Constructor method for the controller
public ProfileTabUserController(ApexPages.StandardController stdController) {
c = stdController;
subjectID = getTargetSFDCUID();
// If we're operating inside a tab running inside of a profile...
if (subjectID != null) {
// Inject the sfdc.userId URL parameter value into the id param
// so the std User controller loads the right User record
ApexPages.currentPage().getParameters().put('id', subjectID);
}
// Load the User record for the user whose profile we’re viewing
this.subjectUser = (User)stdController.getRecord();
Id viewer = Id.valueOf(UserInfo.getUserId());
Id subject = Id.valueOf(subjectID);
viewingMyProfile = (viewer == subject);
viewerID = UserInfo.getUserId();
}
// Fetches URL parameter passed into a profile tab indicating which user is being viewed
private String getTargetSFDCUID() {
return
ApexPages.currentPage().getParameters().get('sfdc.userId');
}
// Overrides StandardController save method to force reload of current page afterwards
public PageReference save() {
c.save();
return ApexPages.currentPage();
}
// Overrides StandardController cancel method to force page reload
public PageReference cancel() {
c.cancel();
return ApexPages.currentPage();
}
}
But I need help with a test class. I have this but I keep getting error and it won't let me save. Can anyone help me fix the test class or help me to create a new one? Thanks in advance, here is the test class.
@isTest
private class ProfileTabControllerTests {
private static testMethod void testProfileTabController() {
User u = [select Id from User where Id = :UserInfo.getUserId() ];
PageReference pageRef = Page.Chatter_Custom;
Test.setCurrentPage(pageRef);
ApexPages.StandardController con = new ApexPages.StandardController(u);
ProfileTabUserController ext = new ProfileTabUserController(con);
System.assertEquals(u.Id, ext.subjectID);
System.assert(viewingMyProfile);
String cancelPage = ext.cancel().getUrl();
String savePage = ext.save().getUrl();
}
}
Sorry about that.
public boolean viewingMyProfile { get; set; }