+ Start a Discussion
westofoxleywestofoxley 

Apex Test Class Error after Error

Hi I have spent around 40 hours trying to write a test class and when I remove one error I get another.  

I have set up a simple APEX class that is an extension for a Visualforce page. The VF page has a commandButton that saves and takes you to a different page called "Congratulations.vfp"

When I save my test I get this error:

Error: Compile Error: Variable does not exist: controller.Account.Joe_Test__c at line 15 column 9

Here is my Code:

APEX CLASS --

public class linkToVFP {
private ApexPages.StandardController controller;
public linkToVFP(ApexPages.StandardController controller) {
this.controller = controller;
}

  public PageReference saveAndCongrat() {
  controller.save(); // This takes care of the details for you.
  PageReference congratsPage = Page.Congratulations;
  congratsPage.setRedirect(true);
  return congratsPage;
}
}


VF Page --

<apex:page standardController="Account" extensions="linkToVFP">
    <apex:form >
        <apex:inputCheckbox value="{!Account.Joe_Test__c}" selected="" />
        <apex:commandButton value="Go To New Page" action="{!saveAndCongrat}"/>
    </apex:form>
</apex:page>

Test --

@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);
        controller.Account.Joe_Test__c = true;
        PageReference newPage = controller.saveAndCongrat();

        Account newAccount = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, a.Joe_Test__c);

        system.assertEquals(Page.Congratulations, newPage);
    }
}

It is probably something simple that I am missing but I havent been working with Apex for long and I have come from a HTML/CSS/PHP background so you might need to dumb it down a little for me.

Thank you.
Best Answer chosen by westofoxley
James LoghryJames Loghry
@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);

        a.Joe_Test__c = true;
        PageReference newPage = controller.saveAndCongrat();

        Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, results.Joe_Test__c);

       System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
       //system.assertEquals(Page.Congratulations, newPage);
    }
}

I've done a few basic things to your test method to address the compile error(s):

  1. You had a misspelling with your controller.Account.Joe_Test__c because there was no variable in your class called controller.  Furthermore, you can't easily get to the Account through the controller without doing a bunch of casting, etc.  Instead, if you simply modify the account in your unit test, the changes will get propogated into the StandardController.  This is similar to the way it works when you change the value on your VF page as well.  I've tried this on my own dev work to verify that it works.
  2. Kudos to having multiple System.asserts.  You wouldn't believe how many examples there are with little or no System.asserts in unit tests.  I modified your last assert to use getUrl() instead of the PageReference object.  Comparing the URL string instead of the object reference is more accurate, at least in my mind.

All Answers

Jean-NoelJean-Noel
In your test class, the line is bold try to use an undeclared variable called controller, the following one have the same issue.

public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);
        controller.Account.Joe_Test__c = true;
        PageReference newPage = controller.saveAndCongrat();

        Account newAccount = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, a.Joe_Test__c);

        system.assertEquals(Page.Congratulations, newPage);
    }
}
James LoghryJames Loghry
@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);

        a.Joe_Test__c = true;
        PageReference newPage = controller.saveAndCongrat();

        Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, results.Joe_Test__c);

       System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
       //system.assertEquals(Page.Congratulations, newPage);
    }
}

I've done a few basic things to your test method to address the compile error(s):

  1. You had a misspelling with your controller.Account.Joe_Test__c because there was no variable in your class called controller.  Furthermore, you can't easily get to the Account through the controller without doing a bunch of casting, etc.  Instead, if you simply modify the account in your unit test, the changes will get propogated into the StandardController.  This is similar to the way it works when you change the value on your VF page as well.  I've tried this on my own dev work to verify that it works.
  2. Kudos to having multiple System.asserts.  You wouldn't believe how many examples there are with little or no System.asserts in unit tests.  I modified your last assert to use getUrl() instead of the PageReference object.  Comparing the URL string instead of the object reference is more accurate, at least in my mind.
This was selected as the best answer
Virendra ChouhanVirendra Chouhan
Hi westofoxley,

I think you have to use customController insted of controller on line number 15

controller.Account.Joe_Test__c = true;
replace it with  customController.Account.Joe_Test__c = true;

i hope it'll work 

Regards 
Viru
westofoxleywestofoxley

Hi James,

Thank you for helping (and Virendra/Jean).  Thank you for giving me such a helpful explanination also.

I have just tried to use your code and I am now getting this error

Error: Compile Error: Variable does not exist: standardController at line 17 column 33

17: 
PageReference newPage = controller.saveAndCongrat();


I tried Viru's suggestion too but got this error

Error: Compile Error: Variable does not exist: Account at line 15 column 9


Please help once again.  

James LoghryJames Loghry
You just need to spell your variables correctly.   Are you sure that error is correct and from the same class as the line you are posting?  I see a reference to controller and not standard controller as the error suggests.  Would help if you could repost your newer code too.
westofoxleywestofoxley
Hi James,

Here is my new code.  

@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);

        a.Joe_Test__c = true;
        PageReference newPage = controller.saveAndCongrat();

        Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, results.Joe_Test__c);

       System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
       //system.assertEquals(Page.Congratulations, newPage);
    }
}
James LoghryJames Loghry
Your variable is still not spelled correctly.  Change controller.saveAndCongrat to stdController.saveAndCongrat  
westofoxleywestofoxley

Thanks James,

now I am getting this error:
Error: Compile Error: Method does not exist or incorrect signature: [ApexPages.StandardController].saveAndCongrat() at line 17 column 33

This is my new code:

@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);

        a.Joe_Test__c = true;
        PageReference newPage = stdController.saveAndCongrat();

        Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, results.Joe_Test__c);

       System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
       //system.assertEquals(Page.Congratulations, newPage);
    }
}

James LoghryJames Loghry
Sorry, it should have been "customController" not "stdController".  Look, you're going to have to debug these yourself.  Misspellings and Typos are probably the easiest "bug" to fix.  If you can't figure out these errors on your own, then you are in for a world of hurt when it comes to development down the road.
westofoxleywestofoxley
Hi James.  I would just like to say Thank you for all your help.  I just updated it and what do you know, 100% code covered!  
Here is the code that worked FYI.  Thanks again.

@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Congratulations;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);

        a.Joe_Test__c = true;
        PageReference newPage = customController.saveAndCongrat();

        Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, results.Joe_Test__c);

       System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
       //system.assertEquals(Page.Congratulations, newPage);
    }
}