0

I'm trying to define a test method to call a class constructor but somehow it doesn't work

///I create the test data, set Page Parameters etc

     Test.setCurrentPageReference(new PageReference('Page.AndroidDev'));
           System.currentPageReference().getParameters().put('AppID', appID);
           System.currentPageReference().getParameters().put('jobID', jobID);

    //Instantiate the object

    AndroidDevController ad = new AndroidDevController();

When I check my coverage, the constructor is not covered. It shows in red.

Here's how my class is -

  public class AndroidDevController 
    {
     public PageReference AndroidDevController()
        {
             //does some data fetch based on page parameters to load the data

        }
    }

I have been able to write test methods for a constructor for other classes. The only difference here is the constructor returns PageReference (which I need to redirect the user to error page in case of any errors or manual change of parameters) . I have written tests for methods that return a PageReference, but cannot get through with Constructor returning Pagereference.

Any suggestions on what I should change to get a test coverage for this kind of constructor?

2

crop1645 is correct: A constructor always returns an instance of the class. It can't return a pageReference. So what you have there is actually not a constructor.

Here's what I'd do: First, write a proper constructor like this:

public AndroidDevController() {
    // constructor-like stuff goes here
}

Then write a separate method to check for any of the errors you mentioned. Make that method return a pageReference, and call it from the page action.

public pageReference errorCheck(){
    // check for errors.
    // if there are errors, return a pageReference.
    // if there are no errors, return null.
}

...and that's why writing unit tests is a good thing! ;) (It caught one for you!)

  • I understand what you are saying. But not having a constructor return a PageReference doesn't redirect to an error page (from the constructor). This works with other methods which can be an action method. I've already tried this and it doesn't redirect right from the constructor. To get this constructor redirect I had to put an action="{!AndroidDevController}" in the <apex:page action="{!AndroidDevController}"/> and have it return a PageReference. Somehow can't get the codecoverage If possible, can you put a small working example that redirects from constructor itself? – Tasnim Dec 25 '14 at 15:59
  • Hmm. I can't think of a way to do that in Apex without returning a pagereference. You could do a redirect in javascript, based on the results of a js remoting function, but that's getting far off the subject. If the problem you're having is getting proper unit test coverage of code that's working otherwise -- and my guess is that it isn't, except by chance -- then you shouldn't re-write your code. You should focus on your unit tests. (Though, again, I think they're trying to tell you something's wrong here!) – Shane Steinfeld Dec 25 '14 at 17:06
  • You may be right. I'll modify my code and find some other way to show an error. – Tasnim Dec 25 '14 at 17:21
  • Wait -- you're just trying to show an error on a page? There are plenty of ways to do that. Why don't you close this question out and post what you're trying to do as a separate question. (Wouldn't want to address that one here. Too far off the topic of the post.) – Shane Steinfeld Dec 25 '14 at 23:23
  • Here it is salesforce.stackexchange.com/questions/61002/… – Tasnim Dec 26 '14 at 0:23
1

First of all, constructors implicitly return an instance of the class. Hence, they can't return a PageReference

You have:

 public class AndroidDevController   {
 public PageReference Android_DeveloperController()  {
         //does some data fetch based on page parameters to load the data
    }
 }

The method Android_DeveloperController() is not a constructor, it is an instance method. Constructor methods are always the same name as the class. Hence, your test class never invokes Android_DeveloperController() and the line is not covered.

All classes have an implicit constructor with no arguments that does not need to be coded -- and hence it doesn't appear in line coverage

  • I'm sorry for the confusion. Actually the method name is same as the Class name, but it returns a Page Reference. I've updated the original question... – Tasnim Dec 25 '14 at 1:03
  • see Shane's answer - he is correct. – cropredy Dec 25 '14 at 5:20

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.