Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
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. –  Samuel De Rycke Jan 10 '13 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 '13 at 22:17
add comment

2 Answers 2

up vote 2 down vote accepted

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();
}    
share|improve this answer
    
Thanks Bob! Works like a charm. You help me cover those two pesky lines preventing me from 100% coverage –  Salesforce Wizard Jan 11 '13 at 16:10
add comment

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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