3

If anyone has a better idea for a subject - feel free to update.

So I'm setting a private variable in my constructor. I also have some logic in place to set that variable. I want to test both positive and negative aspects of my logic in the constructor. Typically I have a public boolean that I use in test to "Force" a set of logic. However, I'm not sure how to use this since in the test I instantiate the constructor and then can set the variable. the Constructor logic has already fired.

Code:

    private final Case c;

private final ID vOwnerID;

public boolean ErrorTest = false;

public Case_ContactUs_Ext(ApexPages.StandardController con) {        
    this.c = (Case)con.getRecord();
    list<Group> Queues = [SELECT DeveloperName,Email,Id,Name,OwnerId,RelatedId,Type 
                          FROM Group WHERE Type = 'Queue' AND DeveloperName = 'CustomerSupportQueue'];
    if(Queues != null && Queues.size()>0 && ErrorTest == false){
        vOwnerID = Queues[0].id;            
    }else{
        list<User> lUser = [SELECT Id,Name,ProfileId,Profile.Name FROM User where Profile.Name = 'System Administrator'];
        vOwnerID = lUser[0].id;
    }
}

So my test:

    static testMethod void testCase_ContactUS_Ext_Error() { 
    Case c = new Case();
    insert c;
    ApexPages.StandardController sc = new ApexPages.standardController(c);
    Case_ContactUs_Ext ext = new Case_ContactUs_Ext(sc);   
    ext.ErrorTest = true;
    PageReference pageRef = Page.ContactUs;
    Test.setCurrentPage(pageRef);   

    ext.SaveCase();
}    

Is there a better way to do this?

2
  • 1
    Haven't given it much tought, but could using a page parameter be an option as alternative to the class variable ? you can set those before initializing the extention class in test code. Jan 10, 2013 at 20:48
  • 1
    Yeah, a page param might be good here. It's also handy because you could test the flow in unit tests via the currentpage as well, which would be more "in time"
    – joshbirk
    Jan 10, 2013 at 22:17

2 Answers 2

2

You can use a static property for this - that can be set before any constructor is executed as its associated with the class rather than a particular instance.

E.g.

private final Case c;

private final ID vOwnerID;

public static boolean ErrorTest = false;

public Case_ContactUs_Ext(ApexPages.StandardController con) {        
    this.c = (Case)con.getRecord();
    list<Group> Queues = [SELECT DeveloperName,Email,Id,Name,OwnerId,RelatedId,Type 
                          FROM Group WHERE Type = 'Queue' AND DeveloperName = 'CustomerSupportQueue'];
    if(Queues != null && Queues.size()>0 && ErrorTest == false){
        vOwnerID = Queues[0].id;            
    }else{
        list<User> lUser = [SELECT Id,Name,ProfileId,Profile.Name FROM User where Profile.Name = 'System Administrator'];
        vOwnerID = lUser[0].id;
    }
}

and in your test:

static testMethod void testCase_ContactUS_Ext_Error() { 
    Case c = new Case();
    insert c;
    Case_ContactUs_Ext.ErrorTest = true;
    ApexPages.StandardController sc = new ApexPages.standardController(c);
    Case_ContactUs_Ext ext = new Case_ContactUs_Ext(sc);   
    PageReference pageRef = Page.ContactUs;
    Test.setCurrentPage(pageRef);   

    ext.SaveCase();
}    
1
  • Thanks Bob! Works like a charm. You help me cover those two pesky lines preventing me from 100% coverage Jan 11, 2013 at 16:10
1

You could add another constructor for use only by your test:

public Case_ContactUs_Ext(ApexPages.StandardController con, boolean errTest) {        
this.ErrorTest = errTest;
this(con);
}

this(con) will call the main constructor as would occur from Visualforce.

Then in your test:

Case_ContactUs_Ext ext = new Case_ContactUs_Ext(scm, true);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .