+ Start a Discussion
Sandesh Vishwakarma 9Sandesh Vishwakarma 9 

Hi guys. Please help me out in writing test class for this HTTP callout code.

public class OpportunityToBeSent {
    @future (callout=true)
    public static void makePostCallout(String recId) {
        String token; 
        //        System.debug('recorddIdd' + recorddIdd);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndpoint('https://XXXXXXXXXXXXXXXXXXXX/authenticate');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Set the body as a JSON object
        request.setBody('{"email": "XXXXX@XXXX.com","password": "XXXXXX"}');
        System.debug('request'+ request);
        HttpResponse response = http.send(request);
        System.debug('response'+ response);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            Map<String, Object> m2 = (Map<String, Object>) results.get('data');
            System.debug('results'+ m2.get('jwtToken'));
            token = (string)m2.get('jwtToken');
            System.debug('token '+ token);
        }
        makePostCalloutt(recId , token);
        
    }
    
    
    public static void makePostCalloutt(String recId , String tokenn) {
        System.debug('Token >>>'+ tokenn);
      
        String body = recId;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndpoint('https://XXXXXXXXXXXXXXXX?id='+ body);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text');
        request.setHeader( 'token', 'Bearer ' + tokenn );
        // Set the body as a JSON object
        request.setBody(body);
        System.debug('request'+ request);
        HttpResponse response = http.send(request);
        System.debug('response'+ response);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
        
    }    
}
Best Answer chosen by Sandesh Vishwakarma 9
Maharajan CMaharajan C
Hi Sandesh,

Please create the below mock apex class first:
@isTest
global class OpportunityMockClass implements HttpCalloutMock {

    global HTTPResponse respond(HTTPRequest req) {
		
		HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
		res.setBody('{"data": {"jwtToken": "321748736487"}}';);
        res.setStatusCode(200);
        return res;
		
    }
}


Apex test class:
@isTest
public class OpportunityToBeSentTest
{
    @isTest
    public static void OppToBeSentTestMethod()
    {
       // Add all the mandatory fields for Account creation 
        Account objAccount = new Account();
        objAccount.Name = 'Test Acc';
        insert objAccount;		
		
        // Add all the mandatory fields for Opportunity creation 
        Opportunity objOpportunity = new Opportunity();
        objOpportunity.Name = 'Test Opp';
        objOpportunity.Accountid = objAccount.id;
        objOpportunity.StageName = 'Prospecting';
        objOpportunity.CloseDate = system.Today()+3;
        insert objOpportunity;
		
		Test.startTest();
			Test.setMock(HttpCalloutMock.class, new OpportunityMockClass());
			OpportunityToBeSent.makePostCallout(objOpportunity.Id);
		Test.StopTest();
    }
}


Thanks,
Maharajan.C