You need to sign in to do that
Don't have an account?

Test Class Help http response
Hello I have a class that I am trying to get some test coverage from that is using a http response. Here is my class
public with sharing class APIApplicationUtility {
public static DTOApplication createApplication (DTOApplication reqApplication){
DTOApplication returnApplication = new DTOApplication();
try{
String endPoint='http://hcscdn-dev.apigee.net/retailapi/v1-dev/application/application';
if(!Test.isRunningTest()){
}
HttpRequest req=new HttpRequest();
req.setEndpoint(endPoint);
req.setMethod('POST');
req.setCompressed(false);
req.setHeader('Content-Type','application/hal+json');
String jsonBody = JSON.serialize(reqApplication);
req.setBody(jsonBody);
//Where going to call the API here
HttpResponse r = OAuthUtility.InvokeService(req,false);
if (r == null){
DTOError er = new DTOError ('There was a problem with the Application service. Please call an administrator.');
returnApplication.errors.add(er);
return returnApplication;
}
String jsonStr = r.getBody();
system.debug('Quote Response: '+jsonStr);
//Deserialize JSON back to DTO class
returnApplication = (DTOApplication)System.JSON.deserialize(jsonStr, DTOApplication.class);
}catch (Exception e){
system.debug(e);
DTOError er = new DTOError(e.getMessage());
returnApplication.errors.add(er);
}
return returnApplication;
}
I wrote a mock also
@isTest
global class TestApplicationAPIMock implements HttpCalloutMock {
global HttpResponse respond(HttpRequest req){
//Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-type', 'application/json');
res.setBody('{"applicationType"}:"major_med"}');
res.setStatusCode(200);
return res;
}
}
And here is the start of my test class
@isTest
public class APIApplicationUtilityTest {
@isTest static void TestAPIApplication(){
Test.setMock(HttpCalloutMock.class, new TestApplicationAPIMock());
APIApplicationUtility thisApp = new APIApplicationUtility();
HttpResponse r = APIApplicationUtility.createApplication();
}
}
Im having problems getting the method.
public with sharing class APIApplicationUtility {
public static DTOApplication createApplication (DTOApplication reqApplication){
DTOApplication returnApplication = new DTOApplication();
try{
String endPoint='http://hcscdn-dev.apigee.net/retailapi/v1-dev/application/application';
if(!Test.isRunningTest()){
}
HttpRequest req=new HttpRequest();
req.setEndpoint(endPoint);
req.setMethod('POST');
req.setCompressed(false);
req.setHeader('Content-Type','application/hal+json');
String jsonBody = JSON.serialize(reqApplication);
req.setBody(jsonBody);
//Where going to call the API here
HttpResponse r = OAuthUtility.InvokeService(req,false);
if (r == null){
DTOError er = new DTOError ('There was a problem with the Application service. Please call an administrator.');
returnApplication.errors.add(er);
return returnApplication;
}
String jsonStr = r.getBody();
system.debug('Quote Response: '+jsonStr);
//Deserialize JSON back to DTO class
returnApplication = (DTOApplication)System.JSON.deserialize(jsonStr, DTOApplication.class);
}catch (Exception e){
system.debug(e);
DTOError er = new DTOError(e.getMessage());
returnApplication.errors.add(er);
}
return returnApplication;
}
I wrote a mock also
@isTest
global class TestApplicationAPIMock implements HttpCalloutMock {
global HttpResponse respond(HttpRequest req){
//Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-type', 'application/json');
res.setBody('{"applicationType"}:"major_med"}');
res.setStatusCode(200);
return res;
}
}
And here is the start of my test class
@isTest
public class APIApplicationUtilityTest {
@isTest static void TestAPIApplication(){
Test.setMock(HttpCalloutMock.class, new TestApplicationAPIMock());
APIApplicationUtility thisApp = new APIApplicationUtility();
HttpResponse r = APIApplicationUtility.createApplication();
}
}
Im having problems getting the method.
createApplication has a parameter which you have not passed . Try passing the DTOApplication parrameter
Also try using
Test.starttest();
APIApplicationUtility.createApplication(paramter);
Test.stoptest();