‘Test Class’ Error:Static methods cannot be invoked through an object instance

I am new to writing test classes. Here is the method I have written my test class on

Class:

public with sharing class caseManage{
    public static string IdSplit(Id i){
        string s = i+'';
        return s.substring(0,s.length()-3);
    }
}

Test Class:

public static testmethod void unitTest1() {
    caseTriggerPostHandler postObj = new caseTriggerPostHandler();
    testDataUtil testHandler = new testDataUtil();        
    Id i;
    Contact con = testHandler.createContact();
    i = con.id;
    postObj.IdSplit(i);
    system.assertEquals(15, 'i');        
}

As I said, I am pretty much new to development (admin backrground) so I am sure there are bunch of errors in my test class. Please suggest me corrections to my test class.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As the error suggests static class cannot be instantiated

Hence you just need to call the method without creating an instance of the class

public static testmethod void unitTest1() {
//caseTriggerPostHandler postObj = new caseTriggerPostHandler();//Comment this no need to instantiate
testDataUtil testHandler = new testDataUtil();        
Id i;
Contact con = testHandler.createContact();
     i = con.id;
   caseTriggerPostHandler.IdSplit(i);//Call the method directly using class.methodname
  system.assertEquals(15, 'i');        
}

Since you are new to apex and Test code do some Trails on TrailHead.

https://developer.salesforce.com/trailhead/module/apex_database


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x