Skip to main content
Hi,

 

I have to write test class for callout.

 

Pleas Help!!

 

Apex class:

 

public with sharing class CalloutService {

 

private static final string url='https://www.google.com/';

 

    

 

    public static void runCalloutServices(){

 

     List<Account> acc=new List<Account>();

 

        List<Task> taskList = new List<Task>();

 

        acc=[Select id, Name From Account where Name='Greenfield'];

 

        for(Account a:acc){

 

            HttpRequest req=new HttpRequest();

 

            req.setEndpoint(url);

 

            req.setMethod('GET');

 

            

 

            Http http=new Http();

 

            HttpResponse res=new HttpResponse();

 

            res=http.send(req);

 

            System.debug(res);

 

            String strRes='';

 

            System.debug('*****Response'+ res.getStatusCode());

 

            

 

            if(res.getStatusCode() == 200){

 

               System.debug('Status Code:'+res.getStatusCode());

 

                    Task task = new Task(

 

                                Subject='Josh',

 

                                Status='In Progress',

 

                                Priority='Normal',

 

                                WhatId=a.Id);

 

                                System.debug(task);

 

                                insert task;

 

                

 

                Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();

 

                mail.setToAddresses(new string[] {'pallaviyemle15@gmail.com'});  

 

                  mail.setSubject('A new Hope');  

 

                  mail.setHtmlBody('Callout');  

 

                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

 

            

 

        }

 

        }

 

    }

 

}

 

Thanks,

 

Pallavi

 

 
2 件の回答
  1. 2020年10月8日 12:14
    Hi,

     

    I have created test class but it is giving 20% code coverage. Please correct me if somewhere going wrong.

     

    Test class:

     

    @isTest

     

    public class CalloutServiceTest {

     

    @isTest

     

         static void testrunCalloutServices(){

     

    RestRequest req=new RestRequest();

     

               RestResponse res=new RestResponse();

     

               res.statusCode=200;

     

    try {

     

                   

     

                       Account acct = new Account(Name='Callout Account');

     

                    insert acct;

     

                    ID acctID = acct.ID;

     

                 if(res.statusCode==200){

     

                    Task task = new Task(

     

                                Subject='Josh',

     

                                Status='In Progress',

     

                                   Priority='Normal',

     

                                WhatId=acctID);

     

                                System.debug(task);

     

                     insert task;}

     

                     } catch(DmlException e) {

     

                         System.debug('An unexpected error has occurred: ' + e.getMessage());

     

                    }

     

             req.addHeader('Content-Type', 'Application/json');

     

             req.requestURI='https://th-apex-http-callout.herokuapp.com/animals';

     

             req.httpMethod='Get';

     

             String strRes='';

     

             req.requestBody=blob.valueOf(strRes);

     

             RestContext.request=req;

     

             RestContext.response=res;

     

             Test.startTest();

     

             CalloutService.runCalloutServices();

     

             Test.stopTest();

     

             }

     

        }

     

    Thanks,

     

    Pallavi
0/9000