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
Kameswara Rao AddepalliKameswara Rao Addepalli 

Why Test class not able to call Static methods even they are called with className.methodName() format?

Here is my Test class

@isTest
public class accountExplorer_Test {

    private static testMethod void test() {
       
        accountExplorer hadlerObj = new accountExplorer();
        hadlerObj.accountCollect();
        Transaction__c tansObj = new Transaction__c();
        tansObj=accountExplorer.transDeatils();
        accountExplorer.transNameHandler();
    }    
}

Here is Apex Class:
public class accountExplorer {
   public accountExplorer(){
        system.debug('Inside Constructor');
    }
    //Non Static method : Variable declaration
    List<Account> accountCollectVar = new List<Account>();
    
    //Non-Static method declaration and its body : Account details displayer
    public Account accountCollect(){
        accountCollectVar = [SELECT ID, Name, AccountNumber FROM Account WHERE name='addepalli' OR name='katru'];
        system.debug('Account Details in non-statis method:' +accountCollectVar);
        return accountCollectVar[1];
     }
    //Static Method : Variables declaration Note: Static method not allowing variables to be declared inside methoid body??  ?
    Static Set<Transaction__c> transDetailsVar = new Set<Transaction__c>();
    Static List<Transaction__c> transDetailsVarList = new List<Transaction__c>();
    
    //Metho declaration with method body : Displays all transaction names and its type
    public static Transaction__c transDeatils(){
        transDetailsVarList = [SELECT Name, TransactionType__c FROM Transaction__c WHERE TransactionType__c = 'deposit'];
        transDetailsVar.addAll(transDetailsVarList);
        system.debug('Transaction details:'+transDetailsVar);
        return transDetailsVarList[1];
       
    } 
   
   // Static method, with string return type : Display transaction Name
   //Static string transName{get;set;}
    public static string transNameHandler(){
            Transaction__c transObject = new Transaction__c();
            transObject = [SELECT Name from Transaction__c WHERE TransactionType__c = 'deposit' LIMIT 1];
            system.debug('Transaction name :' +transObject.name);
            return transObject.name;
        }
}
 
cvuyyurucvuyyuru
You need to insert a transaction record with Transaction Type deposit before calling apex class.
return transDetailsVarList[1]; // In transdetails method, this one may be reuslting an error, because you don't have any records in transaction object.
As this line is throwing an error, it was unable to execute the next line.
Kameswara Rao AddepalliKameswara Rao Addepalli
Hi cvuyyuru,

Thanks for your reply,
The Transaction object has records like below:

Enter or modify a SOQL query below: 
SELECT Name, TransactionType__c FROM Transaction__c WHERE TransactionType__c = 'deposit'
  
Query Results
Returned records 1 - 8 of 8 total records in 0.238 seconds:
 NameTransactionType__c
1T-0008Deposit
2T-0015Deposit
3T-0022Deposit
4T-0009Deposit
5T-0011Deposit
6T-0018Deposit
7T-0020Deposit
8T-0026Deposit
cvuyyurucvuyyuru
HI K,
You need to insert records in test class. 
Test classes don't consider records which are already there in the system.
You need insert transcation record in test class.

Example:
Transaction__c tr = new Transaction__c();
tr.TransactionType__c = 'deposit'
insert tr;
Make sure you map all the required fields.

If you wnat test class to consider existing records, use seeAlldata= true
which will not be a good practice.
@isTest(seeAlldata=true)
 
Kameswara Rao AddepalliKameswara Rao Addepalli
Hi C,

I have 3 required Fields among which 'Account_Name__c ' is a MD based Picklist field. Can that be inserted like below? 
       tansObj.Account_Name__c ='David';
        tansObj.TransactionType__c='deposit';
        tansObj.TransAmount__c=123.00;

Thanks in advance,
K