function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SahajSahaj 

Code coverage of apex callout/test class

Hi All,
I am trying to cover the mentioned callout but I am able to achieve 59%. I am unable to cover first method ie getMoog and  all the if statements.
I know there are lots of errors in my code as I am new to technology but currently its working for me, please suggest me to make my code more and more mature.
Global class Moogsoft {
    @future (callout=true)
    public static  void getMoog(String inc, Decimal moog,id iid) {
        HttpResponse  res = authToken();
        String authT;
        JSONParser parser = JSON.createParser(res.getBody());
            while (parser.nextToken() != null) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME){
                    String fieldName = parser.getText();
                    parser.nextToken();
                     if(fieldName == 'auth_token' ) {
                        authT = parser.getText();
                        system.debug('++++++++++'+authT);
                    }
                }
            }
        system.debug('####'+authT);
        HttpResponse  firstc = firstCall(inc,moog,authT);
        system.debug('####first'+firstc.getbody());
        HttpResponse secondC = secondCall(inc,moog,authT);
        system.debug('####'+secondC);
        HttpResponse thirdC = thirdCall(iid,moog,authT);
        system.debug('####'+thirdC);
        
    }
     public static httpResponse authToken() {
        String URL1 = 'abc.com';
        HttpResponse resData = HTTPCallout(URL1, 'GET');
        System.debug('Response from Moog: ('+resData.getStatusCode()+')'+resData.getBody());
           if(resData.getStatusCode()>299) {
            String error = 'Failed getting a request token. HTTP Code = '+resData.getStatusCode()+
                            '. Message: '+resData.getStatus()+'. Response Body: '+resData.getBody();
            system.debug('failed'+error);
               return resdata;
            } 
         
          else {
            return resData;
            }
     }
    public static httpResponse firstCall(String a1,Decimal b1,String c1) {
        String aa = a1;
        Decimal bb = b1;
        String cc = c1;
        System.debug('####'+cc);
        System.debug('####'+aa);
        String URL2 = 'abc.com'';
        HttpResponse res = HTTPCallout(URL2, 'POST');
        System.debug('Response from Code request: ('+res.getStatusCode()+')'+res.getBody());

        if(res.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+res.getStatusCode()+
                        '. Message: '+res.getStatus()+'. Response Body: '+res.getBody();
            System.debug('##### Failed: '+error);
            return res;
        }
        
        
        return res;
        
        }

    public static httpResponse secondCall(String a2,Decimal b2,String c2) {
        String aa = a2;
        Decimal bb = b2;
        String cc =  c2;
        
        
     String URL3 = 'abc.com';
        
       
        HttpResponse resT = HTTPCallout(URL3, 'POST');
        System.debug('Response from Code request: ('+resT.getStatusCode()+')'+resT.getBody());

        if(resT.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+resT.getStatusCode()+
                        '. Message: '+resT.getStatus()+'. Response Body: '+resT.getBody();
            System.debug('##### Failed: '+error);
            return resT;
        }
        
        
         system.debug('success');
        return resT;
             
    }
   
    public static httpResponse thirdCall(id a2,Decimal b2,String c2) {
        id aa = a2;
        Decimal bb = b2;
        String cc =  c2;
        System.debug('3rd call');
        
      String URL3 = 'abc.com';
       
        HttpResponse resm = HTTPCallout(URL3, 'POST');
        System.debug('Response from Code request: ('+resm.getStatusCode()+')'+resm.getBody());

        if(resm.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+resm.getStatusCode()+
                        '. Message: '+resm.getStatus()+'. Response Body: '+resm.getBody();
            System.debug('##### Failed: '+error);
            return resm ;
        }
        

   
        system.debug('sucessfull');
        return resm;
    }

    public Static HttpResponse HTTPCallout(String EndPoint, String Method) {
        Http h = new Http();
        HttpRequest req= new HttpRequest();
        req.setEndpoint(EndPoint);
        req.setMethod(Method);
        HttpResponse res = null;
        res = h.send(req);
        return res;
    }
}
Below is my test class.
@isTest
public class MoogsoftTest {
   static testMethod void moogcallout(){
        // Create the mock response based on a static resource
    StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('TestResponse1');
        mock.setStatusCode(200);
        mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
       // Associate the callout with a mock response
      Test.setMock(HttpCalloutMock.class, mock);
       // Call method to test
      
        HttpResponse result = Moogsoft.authToken();
       // Verify mock response is not null
     System.assertNotEquals(null,result, 'The callout returned a null response.');
      // String error = 'Failed getting a request token. HTTP Code = '+result.getStatusCode()+
           //                 '. Message: '+result.getStatus()+'. Response Body: '+result.getBody();
           // system.debug('failed'+error);
     System.assertEquals(200,result.getStatusCode(),result);
       
       HttpResponse res = Moogsoft.firstCall('ca5c44d8a9df405486f41e8dde87f8fb',12345,'0004321');
       System.assertNotEquals(null,res, 'The callout returned a null response.');
       System.assertEquals(200,res.getStatusCode(),result);
       
       HttpResponse rep = Moogsoft.secondCall('ca5c44d8a9df405486f41e8dde87f8fb',12345,'0004321');
       System.assertNotEquals(null,rep, 'The callout returned a null response.');
       System.assertEquals(200,res.getStatusCode(),result);
       
        HttpResponse rem = Moogsoft.thirdCall('a1TO0000002j3yb',12345,'0004321');
       System.assertNotEquals(null,rem, 'The callout returned a null response.');
       System.assertEquals(200,rem.getStatusCode(),result);
       
       
       
       
       }
}

Please help
Thanks in advance.. :)
 
James LoghryJames Loghry
The trailhead team just released a module on this very topic.  You'll want to create an HttpMockCallout and use that in your unit test.  For more info, see my blog on the new module here: http://codequirk.blogspot.com/2015/12/introducing-newest-trailhead-peak-apex.html