I am trying to test a scheduled class in apex, but upon running the test method it gives me the following error:

System.JSONException: Unexpected character ('0' (code 48)): was expecting double-quote to start field name at [line:1, column:4]

Class.System.JSON.deserialize: line 15, column 1 Class.retrieveGAdata.main: line 18, column 1 Class.scheduledGoogleAnalytics.execute: line 4, column 1

This is my test method:

 public static testMethod void testscheduledGA()

          scheduledGoogleAnalytics sga = new  scheduledGoogleAnalytics(); 
          String sch = '20 30 8 10 2 ?'; 
          system.schedule('Test GA check', sch, sga);



This is my scheduled class:

global class scheduledGoogleAnalytics implements Schedulable {
   global void execute(SchedulableContext SC) {
      retrieveGAdata ga = new  retrieveGAdata(); 

And this is line 18 from retrieveGAdata:

 JsonGA data = (JsonGA) JSON.deserialize(s, JsonGA.class); 

where s is a string response;

  • I am pretty sure that issues is in string "s" -- that is not valid json – kurunve May 9 '16 at 9:16
  • if I run retrieveGAdata everything works perfectly fine, and s is a callout response so it is a valid json. I am just wondering when I am running the test class maybe s doesn't get any response – Marc Zaharescu May 9 '16 at 9:21
  • 2
    yes SF don't make real callout in test classes. You need to handle that. Use mock callout and then pass valid JSON. – Tushar Sharma May 9 '16 at 9:23
  • yeah I was having some issues with mock callouts so I just added some bypassing test conditions in my code, but I guess I forgot to handle the json response. – Marc Zaharescu May 9 '16 at 9:24

The issue was due to the fact that I was not giving a proper json response when the test class was triggered. Adding a mock return json statement helped me solve the problem.

| improve this answer | |

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.