You need to sign in to do that
Don't have an account?
westofoxley
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.
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.
I've done a few basic things to your test method to address the compile error(s):
All Answers
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);
}
}
I've done a few basic things to your test method to address the compile error(s):
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
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.
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);
}
}
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);
}
}
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);
}
}