0

How do I call a method in my test class which is basically been called from the action function on the vf page?

VF page:

<button type="button" onclick="myJSMethod();">my button</button>

<script>
    function myJSMethod() {
       //code
       callControllerMethod(param);
    }
</script>

<apex:actionFunction name="callControllerMethod" action={!myControllerMethod}>
     <apex:param name="param" value="" assignTo="{!someVar}"/>
</apex:actionFunction>

Controller

public with sharing class MyClass {
   public String someVar {get; set;}
   public void myControllerMethod() {
     //code
   }
}

I want to call the myControllerMethod in my test class method.

When I do something like this in my test method

 MyClass class = new MyClass();
 class.myControllerMethod();

I get de-reference null error. Is there a way to call that method?

1

In your test class just use the instance or your controller and call the method directly. Make sure you set the properties appropriately before calling it. Remember, you are testing the class functionality not the page functionality so you will have to replicate the setting of values that the page would do before calling the method.

  • You were right, i was missing the replication of setting the values the page would need before calling the method. – codeinprogress Aug 13 '14 at 19:30
0

~~The way you are calling is correct. The only fault I can see is that you are using class. This is a reserved keyword and it is allowing you to use it. Change it to something like mClass so that it's not longer a reserved word and isn't the same name as the actual class name.~~ No long applies due to comments.

About your error in particular, you don't really give us enough info about it. My guess is that something in the content of that method is causing you have that error thrown. It isn't about the way you are calling it but what you are doing in it likely causing your issue.

  • I am not using the keyword, the actual names are different. The above one is just an example to show my approach. – codeinprogress Aug 13 '14 at 19:21
  • @Neeraj Then only my second paragraph applies. – dphil Aug 13 '14 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.