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

test code
Hi friends,
can any one help me how to write a test code for webservices.
for example,
global class CallYahooQuotes
{
@future(callout=true)
public static void getQuotes()
{ //for our simple example we are going to just update the quotes on one Account
Account acct = [select id, TickerSymbol, stock_price__c from account where name = 'Salesforce.com'];
//where f stands for the fields to be displayed (in this case: s - symbol; l1 - last price; c1 - change; d1 = last date)
// String url = 'http://quote.yahoo.com/d/quotes.csv?s='+acct.TickerSymbol+'&f=sl1c1d1&e=.csv';
String url = 'http://download.finance.yahoo.com/d/quotes.csv?s='+acct.TickerSymbol+'&f=l1&e=.csv';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res = h.send(req);
//omitting error handling for example
acct.stock_price__c = decimal.valueOf(res.getBody().trim());
update acct;
}
}
Thanks,
Srikanth
Hi
Before u run this test class try to create Account record with account name='salesforce.com'.
Try this
@isTest(SeeAllData=True)
pirvate class CallYahooQuotes_test{
static testmethod void CallYahooQuotes_test(){
Account acc = new Account();
acc.name='Salesforce.com';
insert acc;
CallYahooQuotes cyq = new CallYahooQuotes();
CallYahooQuotes.getQuotes();
}}
Regards,
Rajesh.
Hi bro,
its not working.
Thanks,
Srikanth
Hi
Plz accept the solution so that other can use it .
Regards,
Praveen Jha
Salesforce Certified Developer.
Hi friends,
it will show an error like.
Method does not exist or incorrect signature: CallYahooQuotes.getQuotes() at line 11 column 1
Thanks,
Srikanth
Hi
Could u post ur complete class and test class.
Regards,
Rajesh.
Here's the Apex documentation on Testing HTTP Callouts, it should explain what you need to do.
It involves having your class use a fake/mock response when Test.isRunningTest() is true.
A thorough test would involve running your sample class multiple times with various mock responses, to make sure you're properly handling all anticipated response types (like when the server response isn't responding and you get a 500 error, or the content of the response isn't what you expected).