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
KR_ForceKR_Force 

Test class help for web services class(Compile Error: Illegal assignment from void to System.HttpResponse)

at line 25  method is not taking arguments,But my  actual method takes the arugments and throwing the exception on save "Illegal assignment from void to System.HttpResponse"
  1.     
  2.     @isTest
  3.         private class TestEvalEndDateHTTPCallOut {
  4.     
  5.        @isTest static void testCallout() {
  6.             set<ID> ast=new Set<ID>();
  7.             Test.setMock(WebServiceMock.class, new MockHttpResponseGenerator());
  8.             Account a = new Account();
  9.             a.Name = 'test account';
  10.             a.Product_Subscriptions__c='test;test:';
  11.             insert a;
  12.             Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under   Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
  13.             ast.add(a1.id);
  14.           
  15.             Contact foo = new Contact(FirstName = 'Foo', LastName='Bar');
  16.             insert foo;
  17.             Case c = new Case(Subject = 'Test case', ContactId=foo.id,AssetID=a1.id,AccountID=a.id);
  18.             Insert c;
  19.           
  20.             Http h = new Http();      
  21.             HttpRequest req1 = new HttpRequest();
  22.             req1.setEndpoint('http://mygreatservice.com/foo/bar');
  23.             req1.setMethod('POST');
  24.             //HttpResponse res1 = h.send(req1);
  25.             HttpResponse res1=EvalEndDateHTTPCallOut.parseJSONResponseFuture(ast);
  26.             String contentType = res1.getHeader('Content-Type');
  27.             System.assert(contentType == 'application/json');
  28.             String actualValue = res1.getBody();
  29.             String expectedValue = '{"foo":"bar"}';
  30.             System.assertEquals(actualValue, expectedValue);
  31.             System.assertEquals(200, res1.getStatusCode());
  32.         }
  33.     }
  34.  
  35. ============================================================================================
  36. Here is the class...
  37.  
  38. public class HTTPCallOutClass {
  39.  
  40.     private ApexPages.StandardController stdCtrl {get; set;}
  41.     public Asset ast { get; set; }
  42.     public String sn { get; set; }
  43.     //constructor to get the Asset record
  44.     public EvalEndDateHTTPCallOut(ApexPages.StandardController controller){
  45.         ast =  (Asset) controller.getRecord();
  46.         sn = ast.Name;
  47.         System.debug('The Asset record: ' + sn);
  48.     }
  49.     public PageReference parseJSONResponse() {     
  50.         Http httpProtocol = new Http();
  51.         String s=sn;
  52.         Date eDate;
  53.         Asset a = [Select Id, name,EvalEndDate__c from Asset where name=: sn];
  54.         // Create HTTP request to send.
  55.         HttpRequest request = new HttpRequest();
  56.      
  57.         String ENDPOINT=getUrl('test');
  58.         request.setEndpoint(endpoint+s);
  59.         // Set the HTTP verb to GET.
  60.         request.setMethod('GET');
  61.         request.setTimeout(60000);
  62.         // Send the HTTP request and get the response,The response is in JSON format.
  63.         HttpResponse response = httpProtocol.send(request);
  64.         system.debug(LoggingLevel.INFO,'response:'+response);
  65.         if(response!=null && response.getStatusCode()==200){
  66.             Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
  67.             system.debug(LoggingLevel.INFO,'response data:'+data);
  68.             system.debug(LoggingLevel.INFO,'ErrorID:'+(String)data.get('error_id'));
  69.             if(!string.IsBlank((String)data.get('evalDate'))){
  70.             eDate = Date.valueOf((String)data.get('evalDate'));
  71.             }
  72.         }
  73.         if (eDate!= null){
  74.                 system.debug(LoggingLevel.INFO,'GetResponse:'+response.getBody());
  75.                 a.EvalEndDate__c = eDate;
  76.                 update a;
  77.         }
  78.         system.debug('updated asset=' + a);
  79.         return new PageReference('/'+a.id);
  80.         }
  81.       
  82.     @future(callout=true)
  83.     public static void parseJSONResponseFuture(Set<Id> aIds){     
  84.         Http httpProtocol = new Http();
  85.      
  86.         List<Asset>asts=new List<Asset>([Select Id, name,EvalEndDate__c,SerialNumber from Asset where id in : aIds and Status__c='Evaluation']);
  87.         system.debug(LoggingLevel.INFO,'asts:'+asts);
  88.         For( Asset a:asts){
  89.         String s=a.name;
  90.         HttpRequest request = new HttpRequest();
  91.         String ENDPOINT=getUrl('test');
  92.         request.setEndpoint(endpoint+s);
  93.         request.setMethod('GET');
  94.         request.setTimeout(60000);
  95.         try{
  96.             if(!Test.isRunningTest()){
  97.          
  98.                 HttpResponse response = httpProtocol.send(request);
  99.                 system.debug(LoggingLevel.INFO,'response:'+response);
  100.                 Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
  101.                 system.debug(LoggingLevel.INFO,'response data:'+data);
  102.                 Date eDate= Date.valueOf((String)data.get('evalDate'));
  103.            
  104.         if (eDate!= null){
  105.                 system.debug(LoggingLevel.INFO,'GetResponse:'+response.getBody());
  106.                 a.EvalEndDate__c = eDate;
  107.                 update a;
  108.             }
  109.              system.debug('updated asset****=' + a);
  110.             }
  111.         }
  112.         catch (Exception e) {}
  113.         }
  114.         //update asts;
  115.         system.debug('updated assets=' + asts);
  116.     }
  117.      private static string getUrl(string serviceType){
  118.        Integration_Settings__c iProp=Integration_Settings__c.getValues('IntegrationEndPoints');
  119.         if(iprop != null){
  120.         if(serviceType.equalsIgnoreCase('test')){
  121.             return iProp.url__c;}
  122.         return ' ';
  123.         }
  124.         else {
  125.             return ' ';
  126.         }
  127.     }
  128. }
  129. =============================================================================================
  130. 1.parseJSONResponse() methog being invokde from Button
  131. 2. parseJSONResponseFuture() method from trigger
  132. =======================================================
  133. i started to buld the test class but im struck not able to proceed further, here is my test class. any help would be much appreciated.
  134.  
  135.  
  136. @isTest
  137. private class TestEvalEndDateHTTPCallOut {
  138.  
  139.    @isTest static void testCallout() {
  140.         set<ID> ast=new Set<ID>();
  141.         Test.setMock(WebServiceMock.class, new MockHttpResponseGenerator());
  142.         Account a = new Account();
  143.         a.Name = 'test account';
  144.         a.Product_Subscriptions__c='test;test:';
  145.         insert a;
  146.         Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
  147.         ast.add(a1.id);
  148.         Http h = new Http();      
  149.         HttpRequest req1 = new HttpRequest();
  150.         req1.setEndpoint('http://mygreatservice.com/foo/bar');
  151.         req1.setMethod('POST');
  152.         HttpResponse res1 = h.send(req1);
  153.         String contentType = res1.getHeader('Content-Type');
  154.         System.assert(contentType == 'application/json');
  155.         String actualValue = res1.getBody();
  156.         String expectedValue = '{"foo":"bar"}';
  157.         System.assertEquals(actualValue, expectedValue);
  158.         System.assertEquals(200, res1.getStatusCode());
  159.     }
  160. }
Deepak Kumar ShyoranDeepak Kumar Shyoran
First make a call out from your main class and copy the response in a file using the debug logs. And the put the response at line 152 in your  Test class as currently you a trying to get the response using Call out (http. send() method )  and callout can’t be commit in test class.

Visit this for more details
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

Please mark it as best solution to your problem if it does solve your problem.
KR_ForceKR_Force
Hi Deepak,

I have the mockresponse class todo that, called SingleRequestMock. i was able to fix the compile error but im not able to improve the code covrage got struck @18%, any suggestions? here is my updated test class.

@isTest
public class TestEvalEndDateHTTPCallOut{
    public static testmethod void testAccountCallout() {
        SingleRequestMock fakeResponse = new SingleRequestMock(200,
                                                 'Complete',
                                                 '[{"Name": "sForceTest1"}]',
                                                 null);
        Test.setMock(HttpCalloutMock.class, fakeResponse);
        set<ID> ast=new Set<ID>();
        Account a = new Account();
        a.Name = 'test account';
        a.Product_Subscriptions__c='test;test:';
        insert a;
        Asset a1 = new Asset(Name='testAsset', RVBD_Product_Family__c= 'test',IB_Status__c='Under Evaluation',Instance_Number__c='77777777XX',AccountID=a.id);
        ast.add(a1.id);
       
        Contact foo = new Contact(FirstName = 'Foo', LastName='Bar');
        insert foo;
        Case c = new Case(Subject = 'Test case', ContactId=foo.id,AssetID=a1.id,AccountID=a.id);
        Insert c;
        Apexpages.StandardController stdController = new Apexpages.StandardController(a1);
        EvalEndDateHTTPCallOut controller = new EvalEndDateHTTPCallOut(StdController);
        EvalEndDateHTTPCallOut.parseJSONResponseFuture(ast);
       
    }
}
Deepak Kumar ShyoranDeepak Kumar Shyoran
If you use the developer console for that to see which lines are not covered by your test class then you can easily improve your test class.
Please once try with that hope you will be able to complete your test coverage