function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sylvie SerpletSylvie Serplet 

Need help to write Apex test class

Hi all,

When I am writing test class for SOQL query I generally get 100% coverage with very basic lines of code, but for this one I got an error message "Method does not exist or incorrect signature: void getMyPoints() from the type Controller". Could you please help?
Thank you.

APEX Class
public class MyCPDPointsThisYearAPEXController {
     
   @AuraEnabled
    public static CPD_Points_Total__c getMyPoints(Id ID){        
        
        if (ID ==null){
        return [select Id, Name, CPD_Year__c, Total_Points__c from CPD_Points_Total__c where (Contact_ID_Test__c = 1 or Contact_ID_HO_Test__c = 1) and (CreatedDate = THIS_YEAR) LIMIT 1] ;     
        }
        
        List<CPD_Points_Total__c> points = [select Id, Name, CPD_Year__c, Total_Points__c from CPD_Points_Total__c where (Id = :ID) and  (Contact_ID_Test__c = 1 or Contact_ID_HO_Test__c = 1) and (CreatedDate = THIS_YEAR) LIMIT 1] ;     
       
        if (points.size() ==0){
            return [select Id, Name, CPD_Year__c, Total_Points__c from CPD_Points_Total__c where (Contact_ID_Test__c = 1 or Contact_ID_HO_Test__c = 1) and (CreatedDate = THIS_YEAR) LIMIT 1] ;     
        }else {            
            return points[0];
        }          
    }     
}

Test Class
@isTest
public class MyCPDPointsAPEXControllerTest {
    
    static testMethod void MyTest(){  
        
        List<CPD_Points_Total__c> myPoints = new List<CPD_Points_Total__c>();
        
        CPD_Points_Total__c p1 = new CPD_Points_Total__c();
        p1.name='Test Name';
        p1.CPD_Year__c='2017';
        myPoints.add(p1);
        
        CPD_Points_Total__c p2 = new CPD_Points_Total__c();
        p2.name='Test Name2';
        p2.CPD_Year__c='2017';
        myPoints.add(p2);
        
        insert myPoints;   
        
        MyCPDPointsThisYearAPEXController controller = new MyCPDPointsThisYearAPEXController();
        controller.getMyPoints();
    }
}


 
Salesforce DeveloperSalesforce Developer
It's static method, call it with class name :
 MyCPDPointsThisYearApexController.getMyPoints();
 
Sylvie SerpletSylvie Serplet
I have already tried and still got same error message "Method does not exist or incorrect signature: void getMyPoints() from the type MyCPDPointsThisYearApexController". 
jigarshahjigarshah
Sylvie,

The getMyPoints() method is static and requires a parameter of type Id to be passed to it whereas in your call you are not doing so. The error is because the signatures of the method beign called versus the method that actually exists do not match.

If you have no specific CPD_Points_Total__c Record Id value to be passed then modify line # 20 within your Test Class as shown below.
MyCPDPointsThisYearAPEXController.getMyPoints(null);

If you want to pass a specifc CPD_Points_Total__c Record Id value to be passed then modify line # 18 within your Test Class as shown below
List<Database.SaveResult> myPointsSaveResultList = Database.insert(myPoints);

if(myPointsSaveResultList[0].isSuccess()){
    MyCPDPointsThisYearAPEXController.getMyPoints(myPointsSaveResultList[0].getId());
}

Moreover, I see that you are neither using Test.startTest() and Test.stopTest() as well as System.assertEquals() within your test class. I would recommedn you use them. Refer the Apex Developer Guide to understand more on how to use them.

Please do nor forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.