5

I'm using the json2apex (http://json2apex.herokuapp.com/) function to build a class to I can deserialize the JSON created by this call: http://services.pbs.org/callsigns/zip/77050.json

It works nicely (though I do have to replace the $ signs I get back in the results)

But when I use the simple test method it generates, it doesn't cover any of the class...what I am missing?

EDIT: Here's the class the jsontoapex creates, with the testmethod at the end. I'm sure it that test method used to provide coverage for the class, but doesn't seem to any more...

public class StationFinder_bfc {

     public class links {
        public String type;
        public String relationship;
        public String self;
        public String elements;
        public String callsign;
        public String common_name;
        public String short_common_name;
        public List<links_X> links;
                     }

public class links_X {
        public String type;
        public String mailing_state;
        public String short_common_name;
        public String relationship;
        public String self;
        public String mailing_city;
        public String created;
        public String common_name;
        public String edited;
        public String elements;
    }

    public String type;
    public String self;
    public List<items> items;
    public String elements;

    public class items {
        public Integer confidence;
        public String type;
        public Integer rank;
        public String self;
        public List<links> links;
    }


    public static StationFinder_bfc parse(String json) {
        return (StationFinder_bfc) System.JSON.deserialize(json, StationFinder_bfc.class);
    }

    static testMethod void testParse() {
        String json = '{\"$type\": \"application/vnd.pbs-collection+json\", \"$self\": \"http://services.pbs.org/callsigns/zip/77050.json\", \"$items\": [{\"confidence\": 100, \"$type\": \"application/vnd.pbs-resource+json\", \"$class\": \"CallSign2ZipMapping\", \"rank\": 1, \"$self\": \"http://services.pbs.org/callsigns/zip/77050/KUHT.json\", \"$links\": [{\"callsign\": \"KUHT\", \"$links\": [{\"$type\": \"application/vnd.pbs-resource+json\", \"mailing_state\": \"TX\", \"short_common_name\": \"Houston\", \"$class\": \"Station\", \"$relationship\": \"parent\", \"$self\": \"http://services.pbs.org/station/36.json\", \"mailing_city\": \"Houston\", \"$created\": \"2012-11-12T04:54:52-05:00\", \"$links\": [{\"$type\": \"application/vnd.pbs-collection+json\", \"$relationship\": \"children\", \"$self\": \"http://services.pbs.org/callsigns/station/36.json\", \"$elements\": \"Callsign\", \"$accepts\": [{\"$type\": \"application/vnd.pbs-resource+json\", \"$class\": \"Callsign\"}]}, {\"$relationship\": \"flagship\", \"$type\": \"application/vnd.pbs-resource+json\", \"$class\": \"Callsign\", \"callsign\": \"KUHT\", \"$self\": \"http://services.pbs.org/callsign/KUHT.json\"}], \"common_name\": \"KUHT / Channel 8\", \"$edited\": \"2012-11-12T04:54:52-05:00\"}, {\"$type\": \"application/vnd.pbs-collection+json\", \"$relationship\": \"children\", \"$elements\": \"Feed\", \"$self\": \"http://services.pbs.org/feeds/callsign/KUHT.json\"}], \"$type\": \"application/vnd.pbs-resource+json\", \"$class\": \"Callsign\", \"$relationship\": \"related\", \"$self\": \"http://services.pbs.org/callsign/KUHT.json\"}]}], \"$elements\": \"CallSign2ZipMapping\", \"$accepts\": [{\"$type\": \"application/vnd.pbs-resource+json\", \"$class\": \"CallSign2ZipMapping\"}]}';
        String sBody = json.replace('$','');

        StationFinder_bfc obj = parse(sBody);
        System.assert(obj != null);
    }

    }

3 Answers 3

6

Add the {get; set;} accessors to the properties to get coverage.

2
  • Ah - interesting...which raises a second question...is there a way to somehow apply a get to a whole class e.g the inner class called links - or do I have to add a get/set to each public string if I want to display it on a VF page? Commented Mar 26, 2013 at 16:00
  • Don't think so :-( which is especially annoying for properties with default values! Commented Mar 26, 2013 at 17:37
1

You will have worked it out by now, but for anyone else who gets here...You need to instantiate the class and the inner classes and reference them in the test method.

0

You may need to post some actual code, but if you are testing code that is making live web services calls, you will never get coverage because Apex tests do not execute web service callouts. If that's the case, you'll need to mock out the web service call for your test method. Apex has built-in support for Apex2WSDL mock services. The details are here, but if you're getting your JSON from a simple REST service, it's usually just a couple lines of code that allow a live call in normal execution and a mock call in test execution (Test.isTestRunning()).

2
  • Added code above - it's not the callout I am having difficulty with - that works fine - it's getting coverage on the class I use to de-serialize my JSON... Commented Mar 26, 2013 at 6:00
  • So after running that test method, you get 0% coverage showing for this class, with all lines in red? If so, the only possible cause that I can see is that the platform is treating JSON.deserialize like webservice callouts and skipping it for unit tests. That seems unlikely but is probably easily verifiable with some more trial and error - create a test class that does something silly like tests 1+1=2; verify you get 100% coverage; add an additional line that does JSON and see what happens. If it goes to 0, you may be out of luck.
    – jkraybill
    Commented Mar 26, 2013 at 6:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .