0
public class MyController {

    @AuraEnabled
    public static Account getAccount(String accId){
        Account acc = [SELECT Id,Name FROM Account WHERE Id =: accId];
        return acc;
    }
}

@isTest
public class MyController{

    @isTest
    static void getAccountTest(){

    MyController a = new MyController()
    Account acc = a.getAccount();
    
    // verify the output
    System.assertEquals(1, acc.size(), 'expecting to find 1 Account');
    
    }
}



//Method does not exist or incorrect signature: void getAccount() from the type MyController
//Method does not exist or incorrect signature: void size() from the type Account

2 Answers 2

2

Your getAccount() method is static, meaning you don't use an instance of the object to call it, just the class name + method name like MyController.getAccount().

size() is not a method on SObject, but it is a method on collection types (list, set, and map). Instead of asserting size, since you're only returning 1 record, you could assert that acc is not null.

...though if you wouldn't get an account returned by your query, you'd get a NO_ROWS_FOR_ASSIGNMENT exception before you hit that assert.

3

Static methods are called through the class name, not through an "instance" of the class. You're also missing the String parameter. In addition, you need to set up test data, because of database isolation in unit tests. Finally, your assertion is incorrect, as the return value was not a list.

Account testAccount = new Account(Name='Test');
insert testAccount;
Account acc = MyController.getAccount(testAccount.Id);
System.assertEequals(testAccount.Id, accId.Id);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .