You need to sign in to do that
Don't have an account?
Alias Uchiha
How to write a test class for HTTP callout in one test class
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
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
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