function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Holly Havelka 10Holly Havelka 10 

Help with my Extension Controller Test Class

Hi everyone,

I am looking for some help with writing a test class for the below extension controller:
 
/*
 * Extension controller for calling Breakthrough Application Settings.
 *
 * HH, Breakthrough Collaborative, 2017
 */

public class ApplicationDisplayController {

	public Breakthrough_Application_Settings__c settings { get; set; }
	public Boolean isDisplayed { get; set; }

	public ApplicationDisplayController(MyProfileController ctrlParam) {
	    this.settings = [select Display__c,
	                Name 
	              from Breakthrough_Application_Settings__c 
	              where Active__c = true LIMIT 1];
	    this.isDisplayed = settings.Display__c;
	}

	public ApplicationDisplayController(AlumniSearchController ctrlParam) {
	    this.settings = [select Display__c,
	                Name 
	              from Breakthrough_Application_Settings__c 
	              where Active__c = true LIMIT 1];
	    this.isDisplayed = settings.Display__c;
	}

	public ApplicationDisplayController(JobBoardSearchController ctrlParam) {
	    this.settings = [select Display__c,
	                Name 
	              from Breakthrough_Application_Settings__c 
	              where Active__c = true LIMIT 1];
	    this.isDisplayed = settings.Display__c;
	}

	public ApplicationDisplayController(ResourceLibraryController ctrlParam) {
	    this.settings = [select Display__c,
	                Name 
	              from Breakthrough_Application_Settings__c 
	              where Active__c = true LIMIT 1];
	    this.isDisplayed = settings.Display__c;
	}
}

I am just learning Salesforce coding, so I am very shaky on writing test classes.  Thanks in advance for any help or feedback.
Best Answer chosen by Holly Havelka 10
Davy HuijgensDavy Huijgens
Normally a extension appends functionality to a standard controller, you could use the settings in the controller by just retreiving them in the contructor of the controllers which is 1 line of code. 

Should not be more effort then adding an extension to pages. Think that would be a better way to go without knowing your complete requirements.

All Answers

Davy HuijgensDavy Huijgens
Please start by using the following documentation:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

Then if running in to specific issues on your test class create a topic that issue with all your code.

If this answers your question please select it as Best Answer.
Holly Havelka 10Holly Havelka 10
Thanks Davy.

This is my newly constructed test class, but I am getting 'Compile Error: Constructor not defined: [MyProfileController].<Constructor>(Breakthrough_Application_Settings__c)':
 
@isTest
public class ApplicationDisplayControllerTest {

    public static testMethod void isDisplayedTest() {
        Breakthrough_Application_Settings__c setting = new Breakthrough_Application_Settings__c();
        setting.Name = '2017';
        setting.Display__c = true;
        setting.Active__c = true;
        insert setting;

        MyProfileController cc = new MyProfileController(setting);   
        ApplicationDisplayController e = new ApplicationDisplayController(cc);
        system.assertEquals(e.Display__c, setting.Display__c);
    }

}

 
Davy HuijgensDavy Huijgens
Your MyProfileController class does not have a constructor that takes a Breakthrough_Application_Settings__c as a parameter.

This is actually no langer about how to test a controller but a totally different problem or challange.

Your assert has a small issue to, it should be in the format System.assertEquals(Expected, Actual) giving you:
 
System.assertEquals(setting.Display__c, e.isDisplayed);

 
Holly Havelka 10Holly Havelka 10
Davy, so I was trying to not have to modify each controller to add the Breakthrough_Application_Settings__c parameter.  

I have a custom setting list that I want to be able to call on from different visualforce pages.  Instead of adding in the custom setting parameter, I thought I could get away with creating the extension controller that could be easily added to each VF page.

Not sure where to go from here.
Davy HuijgensDavy Huijgens
Normally a extension appends functionality to a standard controller, you could use the settings in the controller by just retreiving them in the contructor of the controllers which is 1 line of code. 

Should not be more effort then adding an extension to pages. Think that would be a better way to go without knowing your complete requirements.
This was selected as the best answer
Holly Havelka 10Holly Havelka 10
Davy Huijens, I went ahead and did as you suggested.  Worked perfectly!  Thanks!