You need to sign in to do that
Don't have an account?
Ty Whitfield
Test function that contains a web service call out
I have an apex function that while doing other tasks, also make a web call out. I am not sure how to write a test for this. I've found documentation for when I need to test a function that returns a HttpResponse but in my case the function returns a PageReference.
Controller Code
My Test Code
I'm getting the error on this line "HttpResponse res = tsc.finalize();" which states "Illegal assignment from System.PageReference to System.HttpResponse" which I understand but don't know how to work-around.
Controller Code
public PageReference finalize() { String specialSerial; Integer i = 0; Integer size = products.size(); test = ''; List<List<String>> productswithSerial = new List<List<String>>{}; .... string serial = generate(prod2.ProductFamilyID__c, String.valueOf(products[i].Pak_Size__c)); ..... } public string generate(decimal productFamily, string pak) { HttpRequest req = new HttpRequest(); HttpResponse res2 = new HttpResponse(); Http http = new Http(); req.setEndpoint('xxxxxxxxxxxxxxxx'); req.setMethod('GET'); res2 = http.send(req); if(res2.getstatusCode() == 200 && res2.getbody() != null){ return res2.getbody(); // test = test + '<br/>' + res2.getbody(); }else{ return 'Invalid'; } }
My Test Code
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); // Call method to test. // This causes a fake response to be sent // from the class that implements HttpCalloutMock. HttpResponse res = tsc.finalize(); // Verify response received contains fake values String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"xxxxxxxxxxxxxxxxxx"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200, res.getStatusCode());
I'm getting the error on this line "HttpResponse res = tsc.finalize();" which states "Illegal assignment from System.PageReference to System.HttpResponse" which I understand but don't know how to work-around.
Abdul Khatri
Since I didn't have your full code so find the below code with few assumption like contoller name, yourPageName etc. Also you also already have mock class which you pick from the salesforce help. Here is simplify your code. Please change the bold ones from above in the class and then run.