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

test class Exception for HTTP Call out
Hello all,
This is my calss:
public class ClS_RecentItem { public string RecOpp{get;set;} public list<opportunity> lstOpp{get;set;} public list<string> name{get;set;} public list<string> ids{get;set;} public list<string> type{get;set;} public NewWrapperClass cls1{get;set;} public ClS_RecentItem () { name= new list<string>(); ids= new list<string>(); Type= new list<string>(); Http httpProtocol = new Http(); HttpRequest request = new HttpRequest(); request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId()); request.setEndPoint('https://cs6.salesforce.com/services/data/v26.0/recent'); //request.setEndPoint('https://ap1.salesforce.com/services/data/v26.0/sobjects/Opportunity'); request.setMethod('GET'); HttpResponse response = httpProtocol.send(request); String jsonInput = response.getBody(); jsonInput = '{ "test1":' + jsonInput + '}'; system.debug('===>'+jsonInput); System.JSONParser parser = JSON.createParser(jsonInput); system.debug('aaaaaa'+parser); System.Type tp1 = System.Type.forName('NewWrapperClass'); system.debug('===)'+tp1+'++++++'+cls1); cls1 = (NewWrapperClass)parser.readValueAs(tp1); System.debug('Wrap class : ' + cls1 ); } } ================================================================================================ This is my test class for the above class. public class test_ClS_RecentItem { @isTest static void testCallout() { Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); HttpResponse res = CalloutClass.getInfoFromExternalService(); String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"ohhh":"good"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200, res.getStatusCode()); NewWrapperClass nwc = New NewWrapperClass(); List<NewWrapperClass> nwcl = New List<NewWrapperClass>(); NewWrapperClass.SubWrapperClass nwcs= New NewWrapperClass.SubWrapperClass(); NewWrapperClass.AttributesWrapper nwcaa = New NewWrapperClass.AttributesWrapper(); ClS_RecentItem recItem = New ClS_RecentItem(); } }
and my Wrapper class is:
public class NewWrapperClass
{
public List<SubWrapperClass> test1{get;set;}
public class SubWrapperClass
{
public AttributesWrapper attributes{get;set;}
public string Id{get;set;}
public string Name{get;set;}
public string CaseNumber{get;set;}
}
public class AttributesWrapper
{
public string type{get;set;}
public string url{get;set;}
}
}
Can some one help me out why i am getting the below exception. But my coverage is 100%
System.JSONException: Expected LIST<NewWrapperClass.SubWrapperClass> but found { at [line:1, column:3]
(This error is pointing to the unlined line in the class.)
Thanks,
Uday
I got out of Exception by Putting the try catch block for the Underlined line like below.
try {
cls1 = (NewWrapperClass)parser.readValueAs(tp1);
} catch (exception e){
system.debug('exe'+e);
}
Thanks,
Uday
All Answers
I got out of Exception by Putting the try catch block for the Underlined line like below.
try {
cls1 = (NewWrapperClass)parser.readValueAs(tp1);
} catch (exception e){
system.debug('exe'+e);
}
Thanks,
Uday
I am also using HttpCallouts to get the lat and longitude values from the Google. Can you paste the MockHttpResponseGenerator and you callout class. I am not able to cover the code upto 75%.
Thanks,
Bujji