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
Rodolfo CalvoRodolfo Calvo 

Cannot call test methods in non-test context

Hello team, 
I have this code: 
public static testMethod PageReference search() 
      {
        try
        {
            //runSearch();
            results = performSearch(searchString);
        }
        catch(Exception e)
        {
            //showMessage = true;
            //message = 'An error has ocurred on public static testmethod PageReference search()';
        }
        return null;
      }
    
      public static List<Account> performSearch(String sString)
      {
        searchString =  sString;
        //String will make a callback of contacs
        String soql = 'select id, name, type, description, website, phone from Account';
        if(searchString != '' && searchString != null)
        soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
        soql = soql + ' limit 25';
        System.debug(soql);
        return database.query(soql); 
      }

All my app is in @isTest
It shows an error: 
Cannot call test methods in non-test context
Error is in expression '{!search}' in component <apex:commandButton> in page lca: External entry point

What am I doing wrong?
Best Answer chosen by Rodolfo Calvo
Rodolfo CalvoRodolfo Calvo
Thanks to all, I solved my self! 
I did this: 
public static list <account> acca {get;set;}  
   public static string searchstring2 {get;set;}  
   /*public lcaController(ApexPages.StandardController controller) {  
   }  */
   public static void search(){  
     string searchquery='select name, id, phone, website, type from account where name like \'%'+searchstring2+'%\' Limit 20';  
     acca= Database.query(searchquery);  
   }

 

All Answers

@Karanraj@Karanraj
Looks like you are calling the search method from your visualforce page which is testmethod. Remove the testmethod, if that particular method is non test method. You can't call the test methods from the non-test class
Rodolfo CalvoRodolfo Calvo
I remove the search(), I replaced it with 'seacrhMethod' and the button still does nothing
Rodolfo CalvoRodolfo Calvo
Cannot call test methods in non-test context
Error is in expression '{!search}' in component <apex:commandButton> in page lca: External entry point
Adrian  GawryszewskiAdrian Gawryszewski
Ok few things. What do you mean the whole app is in @isTest? I assume you mean that this is a test class?

In general test methods are void therefor don't return anything. So if it's test method change PageReference to void and remove return statement. 

if it is a normal class remove the testMethod keyword.

Regards
Adrian
Rodolfo CalvoRodolfo Calvo
Thanks to all, I solved my self! 
I did this: 
public static list <account> acca {get;set;}  
   public static string searchstring2 {get;set;}  
   /*public lcaController(ApexPages.StandardController controller) {  
   }  */
   public static void search(){  
     string searchquery='select name, id, phone, website, type from account where name like \'%'+searchstring2+'%\' Limit 20';  
     acca= Database.query(searchquery);  
   }

 
This was selected as the best answer