0

What is wrong here?

Candidate__c cand = new Candidate__c();
    insert cand;

    PageReference pageRef = Page.UploadCandidatePhotoVF;
        Test.startTest();
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id', cand.Id);
        ApexPages.StandardController stdController = new ApexPages.StandardController(cand);
        FileUploaderController fileUploader = new FileUploaderController(stdController);
        fileUploader.uploadFile();
        Test.stopTest();
4
  • Which line is giving you the error?
    – Robs
    Sep 28, 2018 at 12:57
  • How is your FileUploaderController.uploadFile() declared?
    – Jayant Das
    Sep 28, 2018 at 12:59
  • fileUploader.uploadFile(); Sep 28, 2018 at 13:04
  • declared like public static PageReference Sep 28, 2018 at 13:04

1 Answer 1

2

Based on your comment, you have declared the uploadFile() in your class FileUploadController as a static method, something as below:

 public static PageReference uploadFile() {
 ....
 }

That's what is causing the issue here when you try to call it in your test class as below, when you are invoking a static method using the instance of the class.

FileUploaderController fileUploader = new FileUploaderController(stdController);
fileUploader.uploadFile(); // this is the line which will throw error

Static methods cannot be called using the "instance" of the class, rather they are invoked by using the Class Name directly. So you need to change your code as below to be able to call that method:

FileUploaderController fileUploader = new FileUploaderController(stdController);
FileUploaderController.uploadFile();  // this is how you call static methods.

More details on the documentation, refer to the below snippet from the documentation.

If myStaticMethod() is a static method, myClassInstance.myStaticMethod() is not legal. Instead, refer to those static identifiers using the class: MyClass.myStaticVariable and MyClass.myStaticMethod().

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.