You need to sign in to do that
Don't have an account?
jucuzoglu
How do I test this controller method?
I have a controller extension which has a function similar to
public String getMyvar() { // a bunch of code // returns a string }
I have a test class, but not sure how I can access the variable to force the function to run in my test code.
I have tried something similar to String testVariable = getMyvar(); but that does not work, it says the method does not exist or incorrect signature.
How should I be attempting this?
You need to instanciate your targeted class from your test class - something like:
ClassToBeTested clsTarget = new ClassToBeTested();
String strTestGetString = clsTarget.getMyvar();
All Answers
You need to instanciate your targeted class from your test class - something like:
ClassToBeTested clsTarget = new ClassToBeTested();
String strTestGetString = clsTarget.getMyvar();
create an instance of the class and use like this
system.assertEquals('your expected string value',instance of the class.getmyvar());
When I try to insert this line of code I get an error saying the Contructor is not defined.
One thing I should mention is that my Test class is inside the class being instantiated in this instance.
Gotcha - my post was assuming you had a seperate test class. (Best practice to do so FYI)
My advice would be to:
1) Create a Constructor in your initial class:
2) Pull your existing test code out of your class
3) Create a seperate @isTest Test Class and follow the recommendation from my earlier post.
-Andy
So I used the first suggestion you had, but instantiated the class a bit differently using the solution here: http://boards.developerforce.com/t5/Apex-Code-Development/constructor-not-defined-in-test-method/td-p/164908
In the end I used this code: