0

Here I have @Auraenabled apex class I'm trying to write a test class but it's throwing an error like below

Static method cannot be referenced from a non-static context: List<AuraSampleController.AccountDetails> AuraSampleController.france(String, String)

Method does not exist or incorrect signature: void saveBatchDet(Integer, String, String) from the type AuraSampleController`

My Apex is like below:

public class AuraSampleController{
   public static String language {get;set;}
   Public static  string  NicoPinFce{get;set;}
   public static List<AccountDetails> AccountDetailsList {get;set;}      

@AuraEnabled
    public static List<AccountDetails> france(string NicoPinFce,string QuoteID){

       }

    @AuraEnabled
    public static list<Quote_Line_Item__c> SaveQlis(List<Quote_Line_Item__c> QliList){

    }
  @AuraEnabled
    public static list<batch__c> SaveBatchDet(integer QliRowNum,list<Batch__c> Bt,map<integer,list<batch__c>> Rowmap){

        }

}

Test class:

@isTest
public class testAuraSampleController {  

    @isTest
    Public static void aurasamplemethod(){

          List<String> AccountDetailsList=new List<string>();
          Id QuoteId;

        Test.StartTest(); 

        auraSampleController testAccPlan= new auraSampleController();

        testAccPlan.france(frab,q3.Id);   //first error line I'm getting     
        testAccPlan.saveQlis(QliList); // first error line this one also
        testAccPlan.saveBatchDet(5,'bat','Rowmap');//second error is on this

   Test.StopTest();        
    }
}
1
  • The parameters that are passing to the saveBatchDet method are different in apex class and test class. Your apex class methods are marked as static.. to call that from the test class you don't need an object instance. you can directly call the method without the instance. Commented Sep 4, 2018 at 10:01

2 Answers 2

0

All you need is directly call your method in test class as ClassName.MethodName();

i.e. AuraSampleController.france('first argument', 'second argument');

For the second kind of error, you need to ensure you are passing the argument of the same kind as you have defined in your controller method.

  1. You need to pass Integer
  2. Pass a list of type list
  3. pass a map of type map< integer,list< batch__c>>

First, you need to create appropriate test data according to class for which you are writing test class and insert those in List and Map.

list<Batch__c> bat = new list<Batch__c>();
map< integer,list< batch__c>> Rowmap=new map< integer,list< batch__c>>();

    i.e. testAccPlan.saveBatchDet(5,bat, Rowmap);

Static methods have these characteristics:-

  • They’re associated with a class.
  • They’re allowed only in outer classes.
  • They’re initialized only when a class is loaded.
  • They aren’t transmitted as part of the view state for a Visualforce page.

A static method or variable doesn’t require an instance of the class in order to run.

Before an object of a class is created, all static member variables in a class are initialized, and all static initialization code blocks are executed. These items are handled in the order in which they appear in the class.

A static method is used as a utility method, and it never depends on the value of an instance member variable. Because a static method is only associated with a class, it can’t access the instance member variable values of its class.

Reference:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

1
Static method cannot be referenced from a non-static context: List<AuraSampleController.AccountDetails> AuraSampleController.france(String, String)

Your france and other methods are static. To call that from the test class you don't need an object instance. you can directly call the method without creating the instance. Like : AuraSampleController.france('test1', 'test2');

Method does not exist or incorrect signature: void saveBatchDet(Integer, String, String) from the type AuraSampleController`

The parameters that are passed to the saveBatchDet method are different in apex class and test class.

You must log in to answer this question.

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