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

Web Services in Test Methods

I realize that test methods can't test web services, however it also brings the test itself to a standstill, so that any test operation after the webservice call doesn't run. Basically the test treats the web service as a fatal error. Is there some way to write the call in the test method in such a way as to allow the error to occur, but move to the next step in the test. I tried using a try-catch statement, but I don't think that will work.


If not, is there some way Salesforce can alter this behavior so that the error can be caught, allowing us to add any expected values directly, or return a page reference, and move on with steps after the call??


We've done this in a couple of different ways:


(1) Allow the web service to be notified that its in test mode - e.g. a static boolean variable that defaults to false.  The test method sets this to true and the web service knows not to call out/return fake data etc.

(2) A custom setting that the web service interrogates prior to firing, similar to the above.


See the sample code below :


                                                global class SpecialAccounts


                                                                global class AccountInfo


                                                                WebService String AcctName;

                                                                WebService Integer AcctNumber;


                                                                WebService static Account createAccount(AccountInfo info)


                                                                                Account acct = new Account();

                                                                                acct.Name = info.AcctName;

                                                                                acct.AccountNumber = String.valueOf(info.AcctNumber);

                                                                                insert acct;

                                                                                return acct;


                                                                WebService static Id [] createAccounts(Account parent,Account child, Account grandChild)


                                                                                insert parent;

                                                                                child.parentId = parent.Id;

                                                                                insert child;

                                                                                grandChild.parentId = child.Id;

                                                                                insert grandChild;

                                                                                Id [] results = new Id[3];

                                                                                results[0] = parent.Id;

                                                                                results[1] = child.Id;

                                                                                results[2] = grandChild.Id;

                                                                                return results;


                                                                //test method

                                                                TestMethod static void testAccountCreate()


                                                                                AccountInfo info = new AccountInfo();

                                                                                info.AcctName = 'Manoj Cheenath';

                                                                                info.AcctNumber = 12345;

                                                                                Account acct = SpecialAccounts.createAccount(info);

                                                                                System.assert(acct != null);




Hope this helps.