Search code examples
salesforceapex-code

Apex error: "Save error: Method does not exist or incorrect signature"


I'm currently learning apex (using the Force.com IDE), and I'm running into some trouble when writing a test for a custom controller.

The controller class is as follows:

public with sharing class CustomController {
    private List<TestObject__c> objects;

    public CustomController() {
        objects = [SELECT id, name FROM TestObject__c];
    }

    public List<TestObject__c> getObjects() {
        return objects;
    }
}

and the test class is:

@isTest
private class ControllerTest {
    static testMethod void customControllerTest() {
        CustomController controller = new CustomController();

        System.assertNotEquals(controller, null);

        List<TestObject__c> objects;

        objects = controller.getObjects();

        System.assertNotEquals(objects, null);    
    }
}

On the objects = controller.getObjects(); line I'm getting an error which says:

Save error: Method does not exist or incorrect signature: [CustomController].getObjects()

Anyone have an idea as to why I'm getting this error?


Solution

  • A nice shorthand:

    public List<TestObject__c> objects {get; private set;}
    

    It creates the getter/setter for you and looks cleaner imo. As for your current issue, yes - it's hard saving code directly into production - especially with test classes in separate files.

    Best to do this in a sandbox/dev org then deploy to production (deploy to server - Force.com IDE). But if you must save directly into production then I'd combine test methods with your class. But in the long run, having @test atop a dedicated test class is the way to go. It won't consume your valuable resources this way.