0

This test class is driving me crazy. I have can prove an array is passing through the class from the test class, but I can't seem to get code coverage. I did use the Json2Apex tool to build the class. Any tips comments would be appreciated.

public class JSON2Apex {

    public Integer facilityId;
    public String facilityName;
    public Integer orderId;
    public Integer calculated_Event_Price;
    public Double tax;
    public Integer subTotal;
    public Double orderTotal;
    public Integer tip;
    public Double totalWithTip;
    public Double totalPayments;
    public Integer totalRefunds;
    public Integer balanceDue;
    public String reportFilterStartDate;
    public String reportFilterEndDate;

    public static List<JSON2Apex> parse(String json) {
        return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);

    }
}

Test Class:

@IsTest
public class JSON2Apex_Test {

    static testMethod void testParse() {


        String json = '['+
        '    {'+
        '        \"facilityId\": 15779,'+
        '        \"facilityName\": \"Urban Air - Conway, AB\",'+
        '        \"orderId\": 71989998,'+
        '        \"calculated_Event_Price\": 275,'+
        '        \"tax\": 27.48,'+
        '        \"subTotal\": 314,'+
        '        \"orderTotal\": 341.48,'+
        '        \"tip\": 0,'+
        '        \"totalWithTip\": 341.48,'+
        '        \"totalPayments\": 341.48,'+
        '        \"totalRefunds\": 0,'+
        '        \"balanceDue\": 0,'+
        '        \"reportFilterStartDate\": \"2018-01-01T00:00:00\",'+
        '        \"reportFilterEndDate\": \"2018-01-02T00:00:00\"'+
        '     },'+
        '    {'+
        '        \"facilityId\": 1559,'+
        '        \"facilityName\": \"Urban Air - Conway, AR\",'+
        '        \"orderId\": 71989998,'+
        '        \"calculated_Event_Price\": 275,'+
        '        \"tax\": 27.48,'+
        '        \"subTotal\": 314,'+
        '        \"orderTotal\": 341.48,'+
        '        \"tip\": 0,'+
        '        \"totalWithTip\": 341.48,'+
        '        \"totalPayments\": 341.48,'+
        '        \"totalRefunds\": 0,'+
        '        \"balanceDue\": 0,'+
        '        \"reportFilterStartDate\": \"2018-01-01T00:00:00\",'+
        '        \"reportFilterEndDate\": \"2018-01-02T00:00:00\"'+
        '    }'+
        ']';

        List<JSON2Apex> obj1 = JSON2Apex.parse(json);

        System.assert(obj1 != null);

    }
}
  • Where your post says "Test Class" you just have the same code again. Have you written a Unit Test yet? – Adrian Larson Feb 11 at 4:37
  • Let me fix that – Cameron Houser Feb 11 at 4:37
  • @AdrianLarson, I have attempted to use Unit Test, have not had any success. Is it necessary? – Cameron Houser Feb 11 at 5:00
1

Your class contains only one executable line of code:

return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);

Provided your unit test does not throw an exception, you should have coverage on that line because you explicitly call the containing method JSON2Apex.parse().

The remainder of the class, consisting solely of variable declarations, is not executable. These lines will show in white in the code coverage view because they are not coverable. Only lines shown in blue (covered) and red (uncovered) need to concern you vis-a-vis code coverage.

  • @DavidRead, Thank you for the explanation. That makes sense. Next time I build an API I am going to try to do so from scratch. Tools are great for getting started but I really want to build a better knowledge base. – Cameron Houser Feb 12 at 14:12

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.