Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I have to write test class for this in one test class:-

public static String getMethod(String name) {

        try {

                HttpRequest request = new HttpRequest();

                Http http = new Http();

                HttpResponse response = new HttpResponse();

                request.setMethod('GET');

                request.setEndpoint('https://any_endpoint_here.com');

                response = http.send(request);

                    Map<String, Object> JSONDataMap=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());

                    return String.valueOf(JSONDataMap.get('status'));

                }

            }

        } catch (Exception e) {

            System.debug('Exception>>' + e.getMessage() + ' at Line Number>>' + e.getLineNumber());

        } return null;

    }

    

    @AuraEnabled

  public static String getMethod2(String name2) {

        try {

                HttpRequest request = new HttpRequest();

                Http http = new Http();

                HttpResponse response = new HttpResponse();

                request.setMethod('GET');

                request.setEndpoint('https://any_endpoint_here.com');

                response = http.send(request);

                    Map<String, Object> JSONDataMap=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());

                    return String.valueOf(JSONDataMap.get('status'));

                }

            }

        } catch (Exception e) {

            System.debug('Exception>>' + e.getMessage() + ' at Line Number>>' + e.getLineNumber());

        } return null;

    }

    

    This is the sample code

    Anyone help me in this?

    Thanks

 
1 answer
  1. Jan 8, 2021, 1:03 PM
    Hi Alias,

    First Create the Test Sample data like this:-

        @isTest

        global class MockHttpResponseGenerator implements HttpCalloutMock {

            global HTTPResponse respond(HTTPRequest req){

                try{

                if(req.getEndpoint()=='https://any_endpoint_here.com'){

                    HttpResponse res= new HttpResponse();

                    res.setHeader('Content-Type','application/json');

                    res.setBody('{"Name ":"Test Name"}');

                    res.setStatusCode(200);

                    return res;

                }

            }catch(Exception ex){

                    System.debug('ERROR Message>>>'+ex.getMessage()+' Error Line Number>>> '+ex.getLineNumber());

            }

                return null;

             }

        }

    Below test class works fine please this out:-

    @isTest

        private class TestClassForClassSetupApex {

           @isTest static void  testCallout(){

               Test.setMock(HttpCalloutMock.Class, new MockHttpResponseGenerator());

               Class_Name.getMethod('TestName');

           }

        }

    Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.

    Thanks

    Akshay

     
0/9000