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
jucuzoglujucuzoglu 

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?

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

You need to instanciate your targeted class from your test class - something like:

 

ClassToBeTested clsTarget = new ClassToBeTested();

 

String strTestGetString = clsTarget.getMyvar();

 

 

All Answers

Andy BoettcherAndy Boettcher

You need to instanciate your targeted class from your test class - something like:

 

ClassToBeTested clsTarget = new ClassToBeTested();

 

String strTestGetString = clsTarget.getMyvar();

 

 

This was selected as the best answer
kiranmutturukiranmutturu

create an instance of the class and use like this

 

system.assertEquals('your expected string value',instance of the class.getmyvar());

jucuzoglujucuzoglu

When I try to insert this line of code I get an error saying the Contructor is not defined.

 

ext_PartnerPositionProposal ePPD = new ext_PartnerPositionProposal();

 One thing I should mention is that my Test class is inside the class being instantiated in this instance.

Andy BoettcherAndy Boettcher

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:

 

public ext_PartnerPositionProposal() { }

 

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

jucuzoglujucuzoglu

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:

 

ext_PartnerPositionProposal ePPD = new ext_PartnerPositionProposal(new ApexPages.StandardController(myPPD));
Double myRN = ePPD.getRN();
String myRelink = ePPD.getRelink();