-1

I have a class

public class myclass {

    @AuraEnabled

    public static List <Json2Apex> customerData(String startDate, String enddate){


        List <Json2Apex>  LSData = new List <Json2Apex>();

        String queryString = endpoint;  

        Http http= new Http();
        HttpRequest request= new HttpRequest();
        request.setendpoint(serviceEndPoint + queryString);
        request.setMethod('GET');
        try {        
            if (!Test.isRunningTest()) {
                HttpResponse response = http.send(request);

                if (response.getStatusCode() == 200) {
                    String showval= response.getBody();

                    JSONParser parser = JSON.createParser(showval);
                    while (parser.nextToken() != null) {
                        if ((parser.getCurrentToken() == JSONToken.START_ARRAY)) {
                            while (parser.nextToken() != null) {
                                if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                                    Json2Apex p = (Json2Apex)parser.readValueAs(Json2Apex.class);
                                    LSData.add(p);
                                    parser.skipChildren();
                                }
                            }       
                        }
                    }
                }
            } 
        }catch (Exception ex) {
            SystemLoggingService.log(ex);
        }

        return LSData;
       } 
    } 

My test class is

@isTest

public class Test_myclass {

@isTest static void testCallout() {

        Test.setMock(HttpCalloutMock.class, new MockHttpLabGenerator());

        Account account = CreateTestClassData.createCustomerAccount();

        List<Json2Apex> res = myclass.customerData('2019-10-10','2020-01-01');       
    }
}

Mock Test Class is 

    @isTest
global class MockHttpLabGenerator implements HttpCalloutMock {

    global HTTPResponse respond(HttpRequest request) {

        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('[{"Name":"Test",}]');
        res.setStatusCode(200);
        return res;

    }
}
  • 1
    What is your question? What specific lines are you struggling to cover, and what have you tried to cover them? – David Reed Feb 20 at 3:16
  • Hi David,I am not able to cover the lines :JSONParser parser = JSON.createParser(showval); while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.START_ARRAY)) { while (parser.nextToken() != null) { if (parser.getCurrentToken() == JSONToken.START_OBJECT) { Json2Apex p = (Json2Apex)parser.readValueAs(Json2Apex.class); LSData.add(p); parser.skipChildren(); – Padam Feb 20 at 3:20
0

You won't be able to cover the inner parts because you have this line of code:

if(!Test.isRunningTest()) {

There's no way to bypass this in a unit test, so you can never get inside this block. Remove that, and use the mock class correctly, and you should be able to get close to 100% coverage.

| improve this answer | |

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