I am stuck trying to figure out how to test the following Class: This Class is loaded when a user clicks on a Case detail link and I have an override on View for the Case Object which loads a VF page that then executes the CaseDetailRedirectCode() method below. The method routes the user to the correct case detail page based on their profile.

I have a Test Class so far that creates an Account, Contact and a Community user under said Contact and a new Case owned by said Account and Contact. This is most likely not complicated but I am at a loss on where to go from here.

public class CaseDetailRedirect {

    public Id RecordId { get; set; }

    public CaseDetailRedirect(ApexPages.StandardController stdController) {
        RecordId = stdController.getId();  

    public pagereference CaseDetailRedirectCode() {
      Profile p = [Select Name from Profile where Id =:userinfo.getProfileid()];
      String pname = p.name;

    //check if Profile Name is the Support Community Profile    
      if (pname.equals('Support Community Login user')) {  
        return new PageReference('/CaseDetail?id=' + RecordId);
    } else {       
        return new PageReference('/' + RecordId + '?nooverride=1');


2 Answers 2


First, I'd recommend not using String.equals: it is case sensitive. Just use the normal == equals comparison. Second, I'd recommend using a Custom Label to avoid hard-coding the value in your code. Third, public Id RecordId { get; set; } could also be written as Id RecordId;, unless it needs to appear on the page.

All that aside, to test this, you just need two users to test as. Here's how I might write a test:

@isTest static void test() {
  Case c = new Case( /* required fields here */ );
  insert c;
  CaseDetailRedirect cdr = new CaseDetailRedirect(new ApexPages.StandardController(c));

  User tempUser = new User(
    ProfileId=[SELECT Id FROM Profile WHERE Id = :Label.SupportCommunityLoginUser].Id
    /* and add other required fields */);
  System.runA(tempUser) {
  • if using a Custom Label isn't that just moving where the name is hard coded? I mean it is still hard coded in the label name.
    – Jason
    May 31, 2016 at 21:12
  • @Jason If it's in the code, that means that someone needs to (a) load the code into sandbox, (b) change the code, (c) update the unit tests, (d) pass QA, if needed, (e) run unit tests, and finally (f) deploy to production. If it's in a label, all you need to do is (a) edit a label.
    – sfdcfox
    May 31, 2016 at 21:22
  • Great point, glad I asked the followup question regarding that. Thank You!
    – Jason
    May 31, 2016 at 21:24
  • @Jason You're welcome. Custom labels are a great way to avoid hours of senseless deployments, especially when you start talking about orgs that take an hour to run the tests for a single deployment.
    – sfdcfox
    May 31, 2016 at 21:26

I would do as following:

Case myTestCase; //Assuming this is your properly created and database stored Case object.

ApexPages.StandardController stdCase = new ApexPages.StandardController(myTestCase);
CaseDetailRedirect  ctrExtension=new CaseDetailRedirect(myTestCase);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.