3

I have a generic extention class and a visualforce page I need to write test classes against, but I am stumped. I have tried a few of the examples found here on StackExchange, but with no luck. Any help is greatly appreciated.

Here is my classs:

public with sharing class ExtendObject {
private final SObject so;


public ExtendObject (ApexPages.StandardController controller)
{
this.so = controller.getRecord ();

}
}

And My visualforce page which is a controller extension:

<apex:page standardController="Custom_Object__c" extensions="ExtendObject" showHeader="false" sidebar="false" standardStylesheets="false" >
<apex:tabPanel switchType="client" selectedTab="tab4" id="theTabPanel" rendered="{!Custom_Object__c.Custom_Field__c != null}">
<apex:tab label="Details 4" name="tab4" id="tab4">
<apex:iframe src="http://website.com?this={!Custom_Object__c.Custom_Field__c}&Accept=1" scrolling="true" id="propIframe4"/>

</apex:tab>
<apex:tab label="Details 1" name="tab1" id="tab1">
<apex:iframe src="http://website.com?this={!Custom_Object__c.Custom_Field__c}" scrolling="true" id="propIframe1"/>

</apex:tab>
<apex:tab label="Details 2" name="tab2" id="tab2">
<apex:iframe src="http://website.com?this={!Custom_Object__c.Custom_Field__c}" scrolling="true" id="propIframe2"/>

</apex:tab>
<apex:tab label="Details 3" name="tab3" id="tab3">
<apex:iframe src="http://website.com?this={!Custom_Object__c.Custom_Field__c}" scrolling="true" id="propIframe3"/>

</apex:tab>

<apex:tab label="Details 5" name="tab5" id="tab5">
<apex:iframe src="http://website.com?this={!Custom_Object__c.Custom_Field__c}" scrolling="true" id="propIframe5"/>

</apex:tab>
</apex:tabPanel>
</apex:page>
  • What have you tried so far? With the controller you posted you would only have to call the constructor to cover the entire thing...not much of a test case. Are there any additional methods in your controller that you need help covering? – JCD Feb 28 '13 at 22:37
  • Probably obvious, but I am a bit of a noob. I'm not sure how to just directly call the constructor in a test. All the tests I've written so far are for triggers so I just create the dummy data and let the trigger drive the tests. – user2479 Feb 28 '13 at 22:42
4

You'll want to check out the Salesforce reference on Testing Custom Controllers and Controller Extensions, but here's a bare-bones test case to get you started:

@isTest
private class ExtendObjectTest {

    static testmethod void constructorTest() {
        // set up some test data to work with
        Custom_Object__c co = new Custom_Object__c(Name='blah');
        insert co;

        // start the test execution context
        Test.startTest();

        // set the test's page to your VF page (or pass in a PageReference)
        Test.setCurrentPage(Page.WhateverYourPageIsNamed);

        // call the constructor
        ExtendObject controller = new ExtendObject(new ApexPages.StandardController(co));

        // test action methods on your controller and verify the output with assertions
        controller.save();

        // stop the test
        Test.stopTest();
    }

}
| improve this answer | |
  • I tried something similar earlier and I am still getting the same error: When I call the constructor I get the error Invalid type: ExtendObject Any Ideas – user2479 Feb 28 '13 at 23:28
  • So it turns out I needed to have the test directly within the class. Thanks for your help. I have a lot to learn. – user2479 Mar 1 '13 at 0:19
  • Not that you're likely to run into a problem with this anytime soon, but just an FYI: adding your test methods inside a class not annotated with @isTest will count against your 3MB Apex code limit. It's typically a good idea to keep your test code separate from your functional code (see this question), but there are situations where you might want them together. But in this case, I can't see a reason you would be getting such an error when creating a separate class. – JCD Mar 1 '13 at 5:08
  • JCD, thanks for your help. I'm a bit confused as to why I had to include it in the original class, but it IS now working. Thanks again for taking the time to help out a noob. – user2479 Mar 1 '13 at 5:15

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.