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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Class Test 90% Will not let me get 100% and pass

Hey guys, I am attempting to test my extension. It is getting 90% coverage and yet it is still failing funnily enough (I thought anything over 75% was a pass). I know why it is failing, I am just not sure how to rectify it.

Basically, I have a VF page which is for a survey object that I am creating. The goal is to automatically send out a URL after a client has completed certain milestones. The URL will be part of an email template that will send a link to the site.com hosted version of the VF page with the clients account ID in the URL.. The client will complete the survey and upon clicking save, the survey will be inserted into the clients object by the extension which will draw the account ID from the URL....It all works fine in sandbox, testing just fails as I do not know how to test for AccountID when it gets it from the URL. These are my three codes:

VF page:

<apex:page standardController="Destiny_Survey__c" extensions="extDestinySurvey" >

<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-1.9.1.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-ui-1.10.3.custom.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.jQuery, '/css/ui-lightness/jquery-ui-1.10.3.custom.css')}"/>

<apex:form id="theForm">
<apex:pageBlock id="theBlock">
<apex:pageBlockSection id="theSection1" title="Thank you for completing our Destiny Survey {!Destiny_Survey__c.Account__c}" />
<apex:pageBlockSection id="theSection" columns="1" >
<apex:inputField value="{!Destiny_Survey__c.Which_Product_or_Service__c}" label="Which Destiny Product or Service have you just completed?" id="value3"/>
    <div id="slider" style="length:50"/>
    <apex:inputField value="{!Destiny_Survey__c.How_likely_are_you_to_refer_Destiny__c}" label="How likely is it that you would recommend us to a friend or colleague?" id="value"/>
     <apex:inputField value="{!Destiny_Survey__c.Explain_why_you_gave_your_rating__c}" label="Explain why you gave your rating." id="value2"/>
   
   
</apex:pageBlockSection>
</apex:pageBlock>

<apex:commandButton value="Save" action="{!saveDestinySurvey}"/>
</apex:form>


<script>
    function getCleanName(theName)
    {
        return '#' + theName.replace(/:/gi,"\\:");
    }
    var valueField = getCleanName( '{!$Component.theForm.theBlock.theSection.value}' );

    $j = jQuery.noConflict();
    $j( "#slider" ).slider( {enable: true, min: 0, max: 10, orientation: "horizontal", value: 0} );
    $j( "#slider" ).on( "slidechange",
        function( event, ui )
        {
            var value = $j( "#slider" ).slider( "option", "value" );
            $j( valueField ).val( value );
        }
    );
</script>

</apex:page>




This is my Extension:

public class extDestinySurvey
{
    public Destiny_Survey__c Dess {get;set;}

    private Id AccountId
    {
        get
        {
            if ( AccountId == null )
            {
                String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
                try
                {
                    if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
                }
                catch ( Exception e ) {}
            }
            return AccountId;
        }
        private set;
    }

    public extDestinySurvey(ApexPages.StandardController controller)
    {
        Dess = (Destiny_Survey__c)controller.getRecord();
    }

    public PageReference saveDestinySurvey()
    {
        Dess.Account__c = AccountId;
        upsert Dess;

        // Send the user to the detail page for the new account.
        return new PageReference('/apex/DestinyAccount?id=' + AccountId + '&Sfdc.override=1');
    }
}



This is my Test that fails at 90%:

@isTest
private class TestExtDestinySurvey {

static testMethod void TestExtDestinySurvey(){
        //Testing Survey extension

// create a test account that will be use to reference the Destiny Survey Record




Account acc = new Account(Name = 'Test Account');
insert acc;

//Now lets create Fact Finder record that will be reference for the Standard Account
        Destiny_Survey__c DessTest = new Destiny_Survey__c(Account__c = acc.id, Explain_why_you_gave_your_rating__c = 'Because', How_likely_are_you_to_refer_Destiny__c = 8);
      
  
      
        //call the apepages stad controller
        Apexpages.Standardcontroller stdDess = new Apexpages.Standardcontroller(DessTest);

//now call the class and reference the standardcontroller in the class.
        extDestinySurvey ext = new extDestinySurvey(stdDess);
      
     
//call the pageReference in the class.
        ext.saveDestinySurvey();
    }
  
    }
Best Answer chosen by Developer.mikie.Apex.Student
trsmithtrsmith
In your test, you'll want to set the page reference after inserting your Account record. Then you can add the URL param to the page reference. For example, after you insert acc, add the following:

//Set the page in your test
PageReference reference = Page.MyVFPage; //Change MyVFPage to whatever the name of your VF page is.
Test.setCurrentPageReference(reference);

//Add the URL params
reference.getParameters().put('acct', acc.Id);

All Answers

trsmithtrsmith
In your test, you'll want to set the page reference after inserting your Account record. Then you can add the URL param to the page reference. For example, after you insert acc, add the following:

//Set the page in your test
PageReference reference = Page.MyVFPage; //Change MyVFPage to whatever the name of your VF page is.
Test.setCurrentPageReference(reference);

//Add the URL params
reference.getParameters().put('acct', acc.Id);
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you sooo much, weeks of forums posting and no answer, then you swoop in and smash it just like that. You are a legend mate!

trsmithtrsmith
Great! Glad it helped.