3

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.

4

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

  • Thanks Mohith. That perfectly worked. And one more question. I am using JSON generator which creates a JSON of selected fields from case and posts to webservice. so it is in a future method. To write a test class for this scenario, can you please suggest what should I check in system.assert. Thanks – sf_user Aug 9 '15 at 23:34
  • 2
    Test for updated values in future methods. – Mohith Shrivastava Aug 9 '15 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.