0

I have a class I've written that HttpRequest GET to an API and brings back lead information that I'm converting to Contacts (we use them as Inquiries).

I have 2 additional classes, one is a Wrapper class I use to deserialize the JSON response, the other is lets me turn my Wrapper into a list.

I'm using an HttpCalloutMock class to test it, and filling in a few static JSON responses.

Everything works, it's wonderful and my test results are 80% code coverage, but all the lines that aren't tested are all the lines in those 2 classes.

Can anyone tell me why the test ignores those classes?

EDIT: Here's some code: I'm using the SingleRequestMock.cls outlined in this post, I'm also using the testMethod in that post: https://developer.salesforce.com/blogs/developer-relations/2013/03/testing-apex-callouts-using-httpcalloutmock.html

requestmock:

@isTest
public class SingleRequestMock implements HttpCalloutMock {
    protected Integer code;
    protected String status;
    protected String bodyAsString;
    protected Blob bodyAsBlob;
    protected Map<String, String> responseHeaders;

    public SingleRequestMock(Integer code, String status, String body,
                                     Map<String, String> responseHeaders) {
        this.code = code;
        this.status = status;
        this.bodyAsString = body;
        this.bodyAsBlob = null;
        this.responseHeaders = responseHeaders;
    }

    public SingleRequestMock(Integer code, String status, Blob body,
                                     Map<String, String> responseHeaders) {
        this.code = code;
        this.status = status;
        this.bodyAsBlob = body;
        this.bodyAsString = null;
        this.responseHeaders = responseHeaders;
    }

    public HTTPResponse respond(HTTPRequest req) {
        HttpResponse resp = new HttpResponse();
        resp.setStatusCode(code);
        resp.setStatus(status);
        if (bodyAsBlob != null) {
            resp.setBodyAsBlob(bodyAsBlob);
        } else {
            resp.setBody(bodyAsString);
        }

        if (responseHeaders != null) {
             for (String key : responseHeaders.keySet()) {
            resp.setHeader(key, responseHeaders.get(key));
             }
        }
        return resp;
    }
}

testclass:

@isTest
private class testrequestStudyAbroad101 {

    static testMethod void testrequestStudyAbroad() {
        Datetime dt=Datetime.now();
        String dts=dt.format('yyyy-MM-dd');
        SingleRequestMock fakeResponse=new SingleRequestMock(200,'OK','[{/**FAKE STATIC JSON HERE**/}]', null);
        Test.setMock(HttpCallOutMock.class, fakeResponse);
        requestStudyAbroad101.getInq();
    }
}

The "uncovered" wrappers:

public class saJSON {
    public List<saJSONwrapper> saJSON;
}

public class saJSONwrapper {
    String id;  
    String email; 
    String name; //contact.name breaks out into firsname lastname
    String university; 
    String comment; 
    String address; //mailing address breaks out to mailing street, mailing city, mailing state, maling zip, mailing country
    String program_id; //need to map
    String program_name; 
    String program_link; 
    String created_at_string; 
    String catalog_requested; //make a task
    String phone_call_requested; //make a task
    String phone_number; 
    String lead_type; 

}

I'd rather not put in all the extra code because it's just a callout and then a bunch of regex and parsing and soql, and it would take a long time to clean out stuff that shouldn't be on the internet.

3
  • Hard to say without seeing your code. Can you post your classes so we can see what's going on?
    – jongpie
    Nov 13, 2014 at 16:55
  • @jongpie I added some code, if you need the httprequest and all I can add that too but I didn't want to take too long (and cleaning out private company stuff would take me a bit), the bottom of the code I added are the wrappers that are being ignored
    – mikesalty
    Nov 13, 2014 at 17:21
  • a simple debug statement or developer console checkpoint could verify that your mock class's respond() method is returning the JSON string you expect to be usable by your deserialization methods
    – cropredy
    Nov 13, 2014 at 17:54

1 Answer 1

1

Sounds like you are facing the same issue as I posted here - that de-serializing JSON in a test doesn't create code coverage of the class it is deserialized into...the only way I have got round it was to add getters to each element, and get each one after I have created the class from JSON methods...

3
  • of course! I've seen this problem myself and thanks for reminding me
    – cropredy
    Nov 14, 2014 at 17:18
  • So wait, I basically just need to add something like: public String comment {get;set;}; to all of my variables and it'll suddenly count them in code coverage?
    – mikesalty
    Nov 17, 2014 at 14:35
  • I've always had to add a getter, instantiate the class via the JSON parse method JSON2Apex creates, and then do an assert e.g. system.assertequals('This is a comment', myclass.Comment); Nov 17, 2014 at 16:47

You must log in to answer this question.

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