function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Uday KUday K 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
Uday KUday K

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

Uday KUday K

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

This was selected as the best answer
bujjibujji
Hi Udhay,

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